aboutsummaryrefslogtreecommitdiff
path: root/llvm/lib/Object/Archive.cpp
AgeCommit message (Collapse)AuthorFilesLines
2021-08-05[llvm-ar] Fix for handling thin archive with SYM64 and a test case for itRamesh Peri1-1/+1
WHen thin archives are created which have symbol table of type SYM64 then all the tools will not work since they cannot read the files properly. One can reproduce the problem as follows: 1. Take a hello world program and create an archive out of it. The SYM64_THRESHOLD=0 will force the generation of SYM64 symbol table. clang -c hello.cpp SYM64_THRESHOLD=0 llvm-ar crsT mylib.a hello.o 2. Now try to use any of the tools on this mylib.a and it will fail. llvm-nm -M mylib.a THis fix will eliminate these failures. A regression test is created in llvm/test/Object/archive-symtab.test Reviewed By: MaskRay, Ramesh Differential Revision: https://reviews.llvm.org/D107322
2021-05-27[NFC][libObject] clang-format Archive{.h,.cpp}Jordan Rupprecht1-68/+79
In preparation for D100651
2020-12-01static const char *const foo => const char foo[]Fangrui Song1-2/+2
By default, a non-template variable of non-volatile const-qualified type having namespace-scope has internal linkage, so no need for `static`.
2020-03-11[Object] Fix handling of large archive membersJames Henderson1-7/+3
The archive library truncated the size of archive members whose size was greater than max uint32_t. This patch fixes the issue and adds some unit tests to verify. Reviewed by: ruiu, MaskRay, grimar, rupprecht Differential Revision: https://reviews.llvm.org/D75742
2020-02-10Revert "Remove redundant "std::move"s in return statements"Bill Wendling1-4/+4
The build failed with error: call to deleted constructor of 'llvm::Error' errors. This reverts commit 1c2241a7936bf85aa68aef94bd40c3ba77d8ddf2.
2020-02-10Remove redundant "std::move"s in return statementsBill Wendling1-4/+4
2020-01-28Make llvm::StringRef to std::string conversions explicit.Benjamin Kramer1-2/+2
This is how it should've been and brings it more in line with std::string_view. There should be no functional change here. This is mostly mechanical from a custom clang-tidy check, with a lot of manual fixups. It uncovers a lot of minor inefficiencies. This doesn't actually modify StringRef yet, I'll do that in a follow-up.
2019-09-20[Object] Uncapitalize an error messageFangrui Song1-1/+1
Test case will be added by my next commit. llvm-svn: 372369
2019-07-23[llvm-ar] Fix support for archives with members larger than 4GBOwen Reynolds1-2/+2
llvm-ar outputs a strange error message when handling archives with members larger than 4GB due to not checking file size when passing the value as an unsigned 32 bit integer. This overflow issue caused malformed archives to be created.: https://bugs.llvm.org/show_bug.cgi?id=38058 This change allows for members above 4GB and will error in a case that is over the formats size limit, a 10 digit decimal integer. Differential Revision: https://reviews.llvm.org/D65093 llvm-svn: 366813
2019-02-06[libObject][NFC] Include filename in error messageJordan Rupprecht1-1/+1
llvm-svn: 353341
2019-02-05[ADT] Add a fallible_iterator wrapper.Lang Hames1-5/+4
A fallible iterator is one whose increment or decrement operations may fail. This would usually be supported by replacing the ++ and -- operators with methods that return error: class MyFallibleIterator { public: // ... Error inc(); Errro dec(); // ... }; The downside of this style is that it no longer conforms to the C++ iterator concept, and can not make use of standard algorithms and features such as range-based for loops. The fallible_iterator wrapper takes an iterator written in the style above and adapts it to (mostly) conform with the C++ iterator concept. It does this by providing standard ++ and -- operator implementations, returning any errors generated via a side channel (an Error reference passed into the wrapper at construction time), and immediately jumping the iterator to a known 'end' value upon error. It also marks the Error as checked any time an iterator is compared with a known end value and found to be inequal, allowing early exit from loops without redundant error checking*. Usage looks like: MyFallibleIterator I = ..., E = ...; Error Err = Error::success(); for (auto &Elem : make_fallible_range(I, E, Err)) { // Loop body is only entered when safe. // Early exits from loop body permitted without checking Err. if (SomeCondition) return; } if (Err) // Handle error. * Since failure causes a fallible iterator to jump to end, testing that a fallible iterator is not an end value implicitly verifies that the error is a success value, and so is equivalent to an error check. Reviewers: dblaikie, rupprecht Subscribers: mgorny, dexonsmith, kristina, llvm-commits Tags: #llvm Differential Revision: https://reviews.llvm.org/D57618 llvm-svn: 353237
2019-01-19Update the file headers across all of the LLVM projects in the monorepoChandler Carruth1-4/+3
to reflect the new license. We understand that people may be surprised that we're moving the header entirely to discuss the new license. We checked this carefully with the Foundation's lawyer and we believe this is the correct approach. Essentially, all code in the project is now made available by the LLVM project under our new license, so you will see that the license headers include that license only. Some of our contributors have contributed code under our old license, and accordingly, we have retained a copy of our old license notice in the top-level files in each project and repository. llvm-svn: 351636
2018-05-08Object: Find terminator correctly when reading long filenames in GNU ↵Hans Wennborg1-4/+8
archives (PR37244) The code was previously relying on there being a null terminator somewhere in (or after) the string table, something made less likely by r330786. Differential Revision: https://reviews.llvm.org/D46527 llvm-svn: 331746
2017-10-27Make 32-bit member offset in Archive::Symbol::getMember 64-bitJake Ehrlich1-1/+1
When accessing a member for a symbol with an offset greater than 2^32 - 1 the current Archive::Symbol::getMember implementation will overflow and cause unexpected behavior. This change simply fixes that. In particular if you call "llvm-nm --print-armap" on an archive that has this behavior you'll get an error. Differential Revision: https://reviews.llvm.org/D39379 llvm-svn: 316801
2017-09-20Rename K_MIPS64 to K_GNU64Jake Ehrlich1-8/+8
This patch renames K_MIPS64 to K_GNU64 as part of a change to add support for writing archives with 64-bit indexes in the symbol table. llvm-svn: 313787
2017-06-06Sort the remaining #include lines in include/... and lib/....Chandler Carruth1-1/+1
I did this a long time ago with a janky python script, but now clang-format has built-in support for this. I fed clang-format every line with a #include and let it re-sort things according to the precise LLVM rules for include ordering baked into clang-format these days. I've reverted a number of files where the results of sorting includes isn't healthy. Either places where we have legacy code relying on particular include ordering (where possible, I'll fix these separately) or where we have particular formatting around #include lines that I didn't want to disturb in this patch. This patch is *entirely* mechanical. If you get merge conflicts or anything, just ignore the changes in this patch and run clang-format over your #include lines in the files. Sorry for any noise here, but it is important to keep these things stable. I was seeing an increasing number of patches with irrelevant re-ordering of #include lines because clang-format was used. This patch at least isolates that churn, makes it easy to skip when resolving conflicts, and gets us to a clean baseline (again). llvm-svn: 304787
2017-04-19[Object] Fix some Clang-tidy modernize and Include What You Use warnings; ↵Eugene Zelenko1-17/+34
other minor fixes (NFC). llvm-svn: 300779
2016-11-11Make the Error class constructor protectedMehdi Amini1-3/+3
This is forcing to use Error::success(), which is in a wide majority of cases a lot more readable. Differential Revision: https://reviews.llvm.org/D26481 llvm-svn: 286561
2016-10-24[Object] Replace TimeValue with std::chronoPavel Labath1-4/+3
Summary: Most of the changes are very straight-forward. The only choice I had to make was to use second-precision time points in the Archive classes. I did this because the archive files use that precision in the on-disk representation anyway. Reviewers: rafael, zturner Subscribers: llvm-commits Differential Revision: https://reviews.llvm.org/D25773 llvm-svn: 284974
2016-10-05[Object] Fix a crash in Archive::child_iterator's default constructor.Lang Hames1-4/+7
To be default constructible, Archive::child_iterator needs to be able to construct an Archive::Child with a null parent, however Archive::Child's constructor always dereferenced its Parent argument to compute the remaining archive size. This commit fixes Archive::Child's constructor to only do the size calculation when the parent is non-null. llvm-svn: 283387
2016-09-30[Object] Define Archive::isEmpty().Rui Ueyama1-1/+4
llvm-svn: 282884
2016-08-04Clean up the logic of the Archive::Child::Child() with an assert to know Err ↵Kevin Enderby1-21/+23
is not a nullptr when we are pointed at real data. David Blaikie pointed out some odd logic in the case the Err value was a nullptr and Lang Hames suggested it could be cleaned it up with an assert to know that Err is not a nullptr when we are pointed at real data. As only in the case of constructing the sentinel value by pointing it at null data is Err is permitted to be a nullptr, since no error could occur in that case. With this change the testing for “if (Err)” is removed from the constructor’s logic and *Err is used directly without any check after the assert(). llvm-svn: 277776
2016-08-03Clean up of libObject/Archive interfaces and change the last three uses of ↵Kevin Enderby1-40/+75
ErrorOr<> changing them to Expected<> to allow them to pass through llvm Errors. No functional change. This commit by itself will break the next lld builds.  I’ll be committing the matching change for lld immediately next. llvm-svn: 277656
2016-08-03Reapply "More fixes to get good error messages for bad archives."Vedant Kumar1-12/+50
This reverts commit the revert commit r277627. The build errors mentioned in r277627 were likely caused by an unclean build directory. Sorry for the noise. llvm-svn: 277630
2016-08-03Revert "More fixes to get good error messages for bad archives."Vedant Kumar1-50/+12
This reverts commit r277540. It breaks the build with: ../lib/Object/Archive.cpp:264:41: error: return type of out-of-line definition of 'llvm::object::ArchiveMemberHeader::getUID' differs from that in the declaration Expected<unsigned> ArchiveMemberHeader::getUID() const { ~~~~~~~~~~~~~~~~~~ ^ include/llvm/Object/Archive.h:53:12: note: previous declaration is here unsigned getUID() const; ~~~~~~~~ ^ llvm-svn: 277627
2016-08-02More fixes to get good error messages for bad archives.Kevin Enderby1-12/+50
Fixed the last incorrect uses of llvm_unreachable() in the code which were actually just cases of errors in the input Archives. llvm-svn: 277540
2016-08-01Simplify some code found when it was moved in r277177David Blaikie1-10/+10
llvm-svn: 277394
2016-07-29Think this will fix issues with the error messages generated for ↵Kevin Enderby1-15/+13
malformed-archives.test in r277177 and added back this test which was deleted in r277196 while I tracked down these problems. Changed from constructing Twine's to std::string's as Twine's don't work across statements. Also removed a few unneeded Twine() constructions. Fix the write_escaped() calls to not pass the unintended second argument fixing the warning on the ld-x86_64-win7 bot. llvm-svn: 277223
2016-07-29The next step along the way to getting good error messages for bad archives.Kevin Enderby1-87/+228
As mentioned in commit log for r276686 this next step is adding a new method in the ArchiveMemberHeader class to get the full name that does proper error checking, and can be use for error messages. To do this the name of ArchiveMemberHeader::getName() is changed to ArchiveMemberHeader::getRawName() to be consistent with Archive::Child::getRawName(). Then the “new” method is the addition of a new implementation of ArchiveMemberHeader::getName() which gets the full name and provides proper error checking. Which is mostly a rewrite of what was Archive::Child::getName() and cleaning up incorrect uses of llvm_unreachable() in the code which were actually just cases of errors in the input Archives. Then Archive::Child::getName() is changed to return Expected<> and use the new implementation of ArchiveMemberHeader::getName() . Also needed to change Archive::getMemoryBufferRef() with these changes to return Expected<> as well to propagate Errors up. As well as changing Archive::isThinMember() to return Expected<> . llvm-svn: 277177
2016-07-25Next step along the way to getting good error messages for bad archives.Kevin Enderby1-23/+84
I consulted with Lang Hames on this work, and the goal was to add a bit of "where" in the archive the error occurred along with what the error was. So this step changes ArchiveMemberHeader into a class with a pointer to the archive header and the parent archive. Which allows the methods in the ArchiveMemberHeader to determine which member the header is for to include that information in the error message. For this first step the "where" is just the offset to the member in the archive. The next step will be a new method on ArchiveMemberHeader to get the full name, if possible, to be use in the error message. Which will now be possible as ArchiveMemberHeader contains a pointer to the Archive with its string table and its size, etc. so the full name can be determined from the header if it is valid. Also this change adds the missing checks the archive header is actually contained in the buffer and is not truncated, as well as if the terminating characters are correct in the header. And changes one error message in Archive::Child::getNext() where the name or offset to member is now added. llvm-svn: 276686
2016-07-22[Support] Make ErrorAsOutParameter take an Error* rather than an Error&.Lang Hames1-4/+3
This allows ErrorAsOutParameter to work better with "optional" errors. For example, consider a function where for certain input values it is known that the function can't fail. This can now be written as: Result foo(Arg X, Error *Err) { ErrorAsOutParameter EAO(Err); if (<Error Condition>) { if (Err) *Err = <report error>; else llvm_unreachable("Unexpected failure!"); } } Rather than having to construct an ErrorAsOutParameter under every conditional where Err is known to be non-null. llvm-svn: 276430
2016-07-19Next step along the way to getting good error messages for bad archives.Kevin Enderby1-35/+46
This step builds on Lang Hames work to change Archive::child_iterator for better interoperation with Error/Expected. Building on that it is now possible to return an error message when the size field of an archive contains non-decimal characters. llvm-svn: 276025
2016-07-14[Object] Change Archive::findSym to return an Expected<Optional<Child>>.Lang Hames1-9/+6
As suggested by Rafael in review of D22079 - this was accidentally left out of the final commit (r275316). llvm-svn: 275469
2016-07-14[Object] Re-apply r275316 now that I have the corresponding LLD patch ready.Lang Hames1-22/+26
llvm-svn: 275361
2016-07-14[Object] Revert r275316, Archive::child_iterator changes, while I update lld.Lang Hames1-26/+22
Should fix the bots broken by r275316. llvm-svn: 275353
2016-07-13[Object] Change Archive::child_iterator for better interop with Error/Expected.Lang Hames1-22/+26
See http://reviews.llvm.org/D22079 Changes the Archive::child_begin and Archive::children to require a reference to an Error. If iterator increment fails (because the archive header is damaged) the iterator will be set to 'end()', and the error stored in the given Error&. The Error value should be checked by the user immediately after the loop. E.g.: Error Err; for (auto &C : A->children(Err)) { // Do something with archive child C. } // Check the error immediately after the loop. if (Err) return Err; Failure to check the Error will result in an abort() when the Error goes out of scope (as guaranteed by the Error class). llvm-svn: 275316
2016-07-05Object: support empty UID/GID fieldsSaleem Abdulrasool1-2/+8
Normal archives do not have empty UID/GID fields. However, the Microsoft Import library format is a customized archive (it just uses an alternate symbol index format). When the import library is constructed by lib.exe, the UID and GID fields are left empty. Do not abort on such an input. llvm-svn: 274528
2016-06-29Change Archive::create() from ErrorOr<...> to Expected<...> and updateKevin Enderby1-18/+25
its clients. This commit will break the next lld builds. I’ll be committing the matching change for lld next. llvm-svn: 274160
2016-06-24Make sure Format is always initialized.Rafael Espindola1-0/+5
Should fix the msan bots. llvm-svn: 273679
2016-06-17Add support for Darwin’s static library table of contents with 64-bit ↵Kevin Enderby1-3/+40
offsets to the archive members. Darwin added support in its Xcode 8.0 tools (released in the beta) for static library table of contents with 64-bit offsets to the archive members. The change is very straight forward. The table of contents member is named ___.SYMDEF_64 or "___.SYMDEF_64 SORTED" and same layout is used but with fields using 64 bit values instead of 32 bit values. rdar://26869808 llvm-svn: 273058
2016-05-17Change llvm-objdump, llvm-nm and llvm-size when reporting an object file errorKevin Enderby1-3/+8
when the object is in an archive to use something like libx.a(foo.o) as part of the error message. Also changed llvm-objdump and llvm-size to be like llvm-nm and ignore non-object files in archives and not produce any error message. To do this Archive::Child::getAsBinary() was changed from ErrorOr<...> to Expected<...> then that was threaded up to its users. Converting this interface to Expected<> from ErrorOr<> does involve touching a number of places. To contain the changes for now the use of errorToErrorCode() is still used in one place yet to be fully converted. Again there some were bugs in the existing code that did not deal with the old ErrorOr<> return values.  So now with Expected<> since they must be checked and the error handled, I added a TODO and a comments for those. llvm-svn: 269784
2016-05-02Expose a getFullName for thin archive members.Rafael Espindola1-10/+18
It will be used in lld. llvm-svn: 268226
2016-04-18[NFC] Header cleanupMehdi Amini1-1/+0
Removed some unused headers, replaced some headers with forward class declarations. Found using simple scripts like this one: clear && ack --cpp -l '#include "llvm/ADT/IndexedMap.h"' | xargs grep -L 'IndexedMap[<]' | xargs grep -n --color=auto 'IndexedMap' Patch by Eugene Kosov <claprix@yandex.ru> Differential Revision: http://reviews.llvm.org/D19219 From: Mehdi Amini <mehdi.amini@apple.com> llvm-svn: 266595
2016-04-06Thread Expected<...> up from createMachOObjectFile() to allow llvm-objdump ↵Kevin Enderby1-1/+4
to produce a real error message Produce the first specific error message for a malformed Mach-O file describing the problem instead of the generic message for object_error::parse_failed of "Invalid data was encountered while parsing the file”.  Many more good error messages will follow after this first one. This is built on Lang Hames’ great work of adding the ’Error' class for structured error handling and threading Error through MachOObjectFile construction. And making createMachOObjectFile return Expected<...> . So to to get the error to the llvm-obdump tool, I changed the stack of these methods to also return Expected<...> : object::ObjectFile::createObjectFile() object::SymbolicFile::createSymbolicFile() object::createBinary() Then finally in ParseInputMachO() in MachODump.cpp the error can be reported and the specific error message can be printed in llvm-objdump and can be seen in the existing test case for the existing malformed binary but with the updated error message. Converting these interfaces to Expected<> from ErrorOr<> does involve touching a number of places. To contain the changes for now use of errorToErrorCode() and errorOrToExpected() are used where the callers are yet to be converted. Also there some were bugs in the existing code that did not deal with the old ErrorOr<> return values. So now with Expected<> since they must be checked and the error handled, I added a TODO and a comment: “// TODO: Actually report errors helpfully” and a call something like consumeError(ObjOrErr.takeError()) so the buggy code will not crash since needed to deal with the Error. Note there is one fix also needed to lld/COFF/InputFiles.cpp that goes along with this that I will commit right after this. So expect lld not to built after this commit and before the next one. llvm-svn: 265606
2016-03-31Object: Correctly read thin archives containing absolute paths.Peter Collingbourne1-3/+8
Differential Revision: http://reviews.llvm.org/D18666 llvm-svn: 265065
2016-02-16Simplify users of StringRef::{l,r}trim (NFC)Vedant Kumar1-9/+8
r260925 introduced a version of the *trim methods which is preferable when trimming a single kind of character. Update all users in llvm. llvm-svn: 260926
2015-11-05Reapply r250906 with many suggested updates from Rafael Espindola.Kevin Enderby1-46/+88
The needed lld matching changes to be submitted immediately next, but this revision will cause lld failures with this alone which is expected. This removes the eating of the error in Archive::Child::getSize() when the characters in the size field in the archive header for the member is not a number. To do this we have all of the needed methods return ErrorOr to push them up until we get out of lib. Then the tools and can handle the error in whatever way is appropriate for that tool. So the solution is to plumb all the ErrorOr stuff through everything that touches archives. This include its iterators as one can create an Archive object but the first or any other Child object may fail to be created due to a bad size field in its header. Thanks to Lang Hames on the changes making child_iterator contain an ErrorOr<Child> instead of a Child and the needed changes to ErrorOr.h to add operator overloading for * and -> . We don’t want to use llvm_unreachable() as it calls abort() and is produces a “crash” and using report_fatal_error() to move the error checking will cause the program to stop, neither of which are really correct in library code. There are still some uses of these that should be cleaned up in this library code for other than the size field. The test cases use archives with text files so one can see the non-digit character, in this case a ‘%’, in the size field. These changes will require corresponding changes to the lld project. That will be committed immediately after this change. But this revision will cause lld failures with this alone which is expected. llvm-svn: 252192
2015-11-03This never returns end(), simplify to use Child instead of iterator. NFC.Rafael Espindola1-3/+2
llvm-svn: 251876
2015-10-31Don't store a Child to the first regular member.Rafael Espindola1-8/+17
This is a bit ugly, but has a few advantages: * Archive is now easy to copy since there is no Archive -> Child -> Archive loop. * It makes it clear that we already checked for errors when finding the Child data. llvm-svn: 251750
2015-10-31Simplify handling of archive Symbol tables.Rafael Espindola1-9/+14
We only need to store a StringRef. llvm-svn: 251748