aboutsummaryrefslogtreecommitdiff
path: root/llvm/lib/Object/MachOObjectFile.cpp
AgeCommit message (Collapse)AuthorFilesLines
2025-08-06[llvm-objdump] Fix typo in error messages (#152234)Daniel Rodríguez Troitiño1-2/+2
Some error messages were spelling "children" as "childern".
2025-05-26[MachO] Improve bounds check (#141083)Nikita Popov1-1/+2
The current check may fail if the addition overflows. I've observed failures of macho-invalid.test on 32-bit due to this. Instead, compare against the remaining bytes until the end of the object.
2024-11-29[Support][Error] Add ErrorAsOutParameter constructor that takes an Error by ref.Lang Hames1-1/+1
ErrorAsOutParameter's Error* constructor supports cases where an Error might not be passed in (because in the calling context it's known that this call won't fail). Most clients always have an Error present however, and for them an Error& overload is more convenient.
2024-09-19[LLVM] Use {} instead of std::nullopt to initialize empty ArrayRef (#109133)Jay Foad1-13/+13
It is almost always simpler to use {} instead of std::nullopt to initialize an empty ArrayRef. This patch changes all occurrences I could find in LLVM itself. In future the ArrayRef(std::nullopt_t) constructor could be deprecated or removed.
2024-08-27[llvm] Prefer StringRef::substr to StringRef::slice (NFC) (#106190)Kazu Hirata1-10/+8
S.substr(N, M) is simpler than S.slice(N, N + M). Also, substr is probably better recognizable than slice thanks to std::string_view::substr.
2024-08-25[llvm] Prefer StringRef::substr to StringRef::slice (NFC) (#105943)Kazu Hirata1-5/+5
S.substr(N) is simpler than S.slice(N, StringRef::npos) and S.slice(N, S.size()). Also, substr is probably better recognizable than slice thanks to std::string_view::substr.
2024-08-20[AArch64][MachO] Add ptrauth ABI version to arm64e cpusubtype. (#104650)Ahmed Bougacha1-1/+1
In a mach_header, the cpusubtype is a 32-bit field, but it's split in 2 subfields: - the low 24 bits containing the cpu subtype proper, (e.g., CPU_SUBTYPE_ARM64E 2) - the high 8 bits containing a capability field used for additional feature flags. Notably, it's only the subtype subfield that participates in fat file slice discrimination: the caps are ignored. arm64e uses the caps subfield to encode a ptrauth ABI version: - 0x80 (CPU_SUBTYPE_PTRAUTH_ABI) denotes a versioned binary - 0x40 denotes a kernel-ABI binary - 0x00-0x0F holds the ptrauth ABI version This teaches the basic obj tools to decode that (or ignore it when unneeded). It also teaches the MachO writer to default to emitting versioned binaries, but with a version of 0 (and without the kernel ABI flag). Modern arm64e requires versioned binaries: a binary with 0x00 caps in cpusubtype is now rejected by the linker and everything after. We can live without the sophistication of specifying the version and kernel ABI for now. Co-authored-by: Francis Visoiu Mistrih <francisvm@apple.com>
2024-08-16[AArch64][MachO] Encode @AUTH to ARM64_RELOC_AUTHENTICATED_POINTER.Ahmed Bougacha1-1/+1
This adds MachO support for emission of authenticated pointer relocations. We already support AArch64AuthMCExpr, to represent assembly expressions such as: .quad <symbol>@AUTH(<key>, <discriminator> [, addr]) For example: .quad _g3@AUTH(ib, 1234, addr) These @AUTH expressions lower to a new kind of MachO relocation: ARM64_RELOC_AUTHENTICATED_POINTER (11) The relocation points to the referenced symbol. The other data, describing the signing scheme and original addend (only 32 bits instead of 64), is encoded into the addend (in the relocated location): |63|62|61-51|50-49| 48 |47 - 32|31 - 0| | 1| 0| 0 | key | addr | discriminator | addend |
2024-08-01[MachO] Remove redundant bounds check (#100176)Daniel Bertalan1-5/+0
The condition was duplicated, the correct one for this message would have been `ImportsEnd > SymbolsEnd`. However, this is a subset of `ImportEnd > Symbols` (since `Symbols <= SymbolsEnd`), so it can be removed altogether. I made this thinko in 686d8ce. Note that that change wasn't intended to be permanent, and served as a quick stopgap to facilitate testing chained fixups in LLD before Apple upstreamed their implementation. Fixes #90662 Fixes #87203
2024-07-09[MachO] Loosen boundary check for reading export trie nodes (#96705)Cyndy Ishida1-1/+1
The design of the export trie in macho's is that each node has a variable length payload. When reading nodes, it should be an error if reading the uleb128 puts you beyond the stated node size but not when the stated size goes beyond the known part that was read. resolves: rdar://130310832 This was primarily authored by Nick Kledzik, I added/cleaned up the test cases.
2024-05-30[MachO] Stop parsing past end of rebase/bind table (#93897)Zixu Wang1-14/+18
`MachORebaseEntry::moveNext()` and `MachOBindEntry::moveNext()` assume that the rebase/bind table ends with `{REBASE|BIND}_OPCODE_DONE` or an actual rebase/bind. However a valid rebase/bind table might also end with other effectively no-op opcodes, which caused the parser to move past the end and go into the next table, resulting in corrupted entries or infinite loops.
2024-05-16[llvm] Drop explicit conversions of string literals to StringRef (NFC)Kazu Hirata1-2/+2
We routinely rely on implicit conversions of string literals to StringRef so that we can use operator==(StringRef, StringRef).
2024-05-08[llvm][MachO] Fix integer truncation in rebase/bind parsing (#89337)Zixu Wang1-10/+10
`Count` and `Skip` should use `uint64_t` as they are encoded/decoded using 64-bit ULEB128. In `*_OPCODE_DO_*_ULEB_TIMES_SKIPPING_ULEB`, `Skip` could be encoded as a two's complement for moving `SegmentOffset` backwards. Having a 32-bit `Skip` truncates the encoded value and leads to a malformed `AdvanceAmount` and invalid `SegmentOffset` that extends past valid sections.
2024-05-08[llvm] Use StringRef::operator== instead of StringRef::equals (NFC) (#91441)Kazu Hirata1-2/+2
I'm planning to remove StringRef::equals in favor of StringRef::operator==. - StringRef::operator==/!= outnumber StringRef::equals by a factor of 70 under llvm/ in terms of their usage. - The elimination of StringRef::equals brings StringRef closer to std::string_view, which has operator== but not equals. - S == "foo" is more readable than S.equals("foo"), especially for !Long.Expression.equals("str") vs Long.Expression != "str".
2023-12-11[llvm] Use StringRef::{starts,ends}_with (NFC) (#74956)Kazu Hirata1-5/+5
This patch replaces uses of StringRef::{starts,ends}with with StringRef::{starts,ends}_with for consistency with std::{string,string_view}::{starts,ends}_with in C++20. I'm planning to deprecate and eventually remove StringRef::{starts,ends}with.
2023-11-29[LEB128] Don't initialize error on successAdrian Prantl1-2/+2
This change removes an unnecessary branch from a hot path. It's also questionable API to override any previous error unconditonally.
2023-11-29Revert "[LEB128] Don't initialize error on success"Adrian Prantl1-2/+2
This reverts commit 545c8e009e2b649ef38f7e432ffbc06ba8a9b813.
2023-11-29[LEB128] Don't initialize error on successAdrian Prantl1-2/+2
This change removes an unnecessary branch from a hot path. It's also questionable API to override any previous error unconditonally.
2023-09-05[llvm-nm][MachO] Add support for `MH_FILESET`Antonio Frighetto1-25/+43
Support printing of symbols for MachO of `MH_FILESET` type. This is achieved by extending `dumpSymbolNamesFromObject` to encompass fileset handling, and including an offset in `MachOObjectFile` class to locate embedded MachO headers. Differential Revision: https://reviews.llvm.org/D159294
2023-02-10[NFC][TargetParser] Replace uses of llvm/Support/Host.hArchibald Elliott1-1/+1
The forwarding header is left in place because of its use in `polly/lib/External/isl/interface/extract_interface.cc`, but I have added a GCC warning about the fact it is deprecated, because it is used in `isl` from where it is included by Polly.
2023-02-07[NFC][TargetParser] Remove llvm/ADT/Triple.hArchibald Elliott1-1/+1
I also ran `git clang-format` to get the headers in the right order for the new location, which has changed the order of other headers in two files.
2023-01-05Move from llvm::makeArrayRef to ArrayRef deduction guides - llvm/ partserge-sans-paille1-9/+9
Use deduction guides instead of helper functions. The only non-automatic changes have been: 1. ArrayRef(some_uint8_pointer, 0) needs to be changed into ArrayRef(some_uint8_pointer, (size_t)0) to avoid an ambiguous call with ArrayRef((uint8_t*), (uint8_t*)) 2. CVSymbol sym(makeArrayRef(symStorage)); needed to be rewritten as CVSymbol sym{ArrayRef(symStorage)}; otherwise the compiler is confused and thinks we have a (bad) function prototype. There was a few similar situation across the codebase. 3. ADL doesn't seem to work the same for deduction-guides and functions, so at some point the llvm namespace must be explicitly stated. 4. The "reference mode" of makeArrayRef(ArrayRef<T> &) that acts as no-op is not supported (a constructor cannot achieve that). Per reviewers' comment, some useless makeArrayRef have been removed in the process. This is a follow-up to https://reviews.llvm.org/D140896 that introduced the deduction guides. Differential Revision: https://reviews.llvm.org/D140955
2022-12-13[llvm][Object] set SF_Hidden flag for MachO filesCyndy Ishida1-2/+6
Reviewed By: pete, ributzka Differential Revision: https://reviews.llvm.org/D139862
2022-12-10Don't include None.h (NFC)Kazu Hirata1-1/+0
I've converted all known uses of None to std::nullopt, so we no longer need to include None.h. This is part of an effort to migrate from llvm::Optional to std::optional: https://discourse.llvm.org/t/deprecating-llvm-optional-x-hasvalue-getvalue-getvalueor/63716
2022-12-09Revert D139098 "[Alignment] Use Align for ObjectFile::getSectionAlignment"Guillaume Chatelet1-3/+10
This breaks lld. This reverts commit 10c47465e2505ddfee4e62a2ab2e535abea3ec56.
2022-12-09[Alignment] Use Align for ObjectFile::getSectionAlignmentGuillaume Chatelet1-10/+3
Differential Revision: https://reviews.llvm.org/D139098
2022-12-04[llvm] Use std::nullopt instead of None in comments (NFC)Kazu Hirata1-1/+1
This is part of an effort to migrate from llvm::Optional to std::optional: https://discourse.llvm.org/t/deprecating-llvm-optional-x-hasvalue-getvalue-getvalueor/63716
2022-12-04[Object] llvm::Optional => std::optionalFangrui Song1-2/+2
2022-12-02[llvm] Use std::nullopt instead of None (NFC)Kazu Hirata1-16/+16
This patch mechanically replaces None with std::nullopt where the compiler would warn if None were deprecated. The intent is to reduce the amount of manual work required in migrating from Optional to std::optional. This is part of an effort to migrate from llvm::Optional to std::optional: https://discourse.llvm.org/t/deprecating-llvm-optional-x-hasvalue-getvalue-getvalueor/63716
2022-11-22[MachO] Support exports trie in both LC_DYLD_INFO and LC_DYLD_EXPORTS_TRIEDaniel Rodríguez Troitiño1-9/+34
The exports trie used to be pointed by the information in LC_DYLD_INFO, but when chained fixups are present, the exports trie is pointed by LC_DYLD_EXPORTS_TRIE instead. Modify the Object library to give access to the information pointed by each of the load commands, and to fallback from one into the other when the exports are requested. Modify ObjectYAML to support dumping the export trie when pointed by LC_DYLD_EXPORTS_TRIE and to parse the existence of a export trie also when the load command is present. This is a split of D134250 with improvements on top. Reviewed By: alexander-shaposhnikov Differential Revision: https://reviews.llvm.org/D134571
2022-09-23[objdump] Fix typo in error message.Daniel Rodríguez Troitiño1-1/+1
Change "inconsistant" for "inconsistent" in the message, the file name and the test output. Reviewed By: pete, MaskRay Differential Revision: https://reviews.llvm.org/D134562
2022-09-08[llvm] Use std::size instead of llvm::array_lengthofJoe Loser1-1/+1
LLVM contains a helpful function for getting the size of a C-style array: `llvm::array_lengthof`. This is useful prior to C++17, but not as helpful for C++17 or later: `std::size` already has support for C-style arrays. Change call sites to use `std::size` instead. Differential Revision: https://reviews.llvm.org/D133429
2022-08-28[llvm-objdump] Add -dyld_info to llvm-otoolDaniel Bertalan1-10/+176
This option outputs the location, encoded value and target of chained fixups, using the same format as `otool -dyld_info`. This initial implementation only supports the DYLD_CHAINED_PTR_64 and DYLD_CHAINED_PTR_64_OFFSET pointer encodings, which are used in x86_64 and arm64 userspace binaries. When Apple's effort to upstream their chained fixups code continues, we'll replace this code with the then-upstreamed code. But we need something in the meantime for testing ld64.lld's chained fixups code. Differential Revision: https://reviews.llvm.org/D132036
2022-08-24[llvm-objdump] Complete -chained_fixups supportDaniel Bertalan1-2/+110
This commit adds definitions for the `dyld_chained_import*` structs. The imports array is now printed with `llvm-otool -chained_fixups`. This completes this option's implementation. A slight difference from cctools otool is that we don't yet dump the raw bytes of the imports entries. When Apple's effort to upstream their chained fixups code continues, we'll replace this code with the then-upstreamed code. But we need something in the meantime for testing ld64.lld's chained fixups code. Differential Revision: https://reviews.llvm.org/D131982
2022-08-18[llvm-objdump] Support dumping segment information with -chained_fixupsDaniel Bertalan1-7/+111
This commit adds the definitions for `dyld_chained_starts_in_image`, `dyld_chained_starts_in_segment`, and related enums. Dumping their contents is possible with the -chained_fixups flag of llvm-otool. The chained-fixups.yaml test was changed to cover bindings/rebases, as well as weak imports, weak symbols and flat namespace symbols. Now that we have actual fixup entries, the __DATA segment contains data that would need to be hexdumped in YAML. We also test empty pages (to look for the "DYLD_CHAINED_PTR_START_NONE" annotation), so the YAML would end up quite large. So instead, this commit includes a binary file. When Apple's effort to upstream their chained fixups code continues, we'll replace this code with the then-upstreamed code. But we need something in the meantime for testing ld64.lld's chained fixups code. Differential Revision: https://reviews.llvm.org/D131961
2022-06-15[llvm] Fix MachO exports trie parsing.Juergen Ributzka1-1/+3
The exports trie parser ordinal validation check doesn't consider the case where the ordinal can be zero or negative for certain special values that are defined in BindSpecialDylib. Update the validation to account for that fact and add a test case. This fixes rdar://94844233. Differential Revision: https://reviews.llvm.org/D127806
2022-03-17[dsymutil] Apply relocations present in Swift reflection sectionsAugusto Noronha1-0/+20
The strippable Swift reflection sections contain subtractor relocations that need to be applied. There are two situations we need to support. 1) Both symbols used in the relocation come from the .o file (for example, one symbol lives in __swift5_fieldmd and the second in __swift5_reflstr). 2) One symbol comes from th .o file and the second from the main binary (for example, __swift5_fieldmd and __swift5_typeref). Differential Revision: https://reviews.llvm.org/D120574
2022-02-25Validate chained fixup image formatsAdrian Prantl1-13/+41
This is part of a series of patches to upstream support for Mach-O chained fixups. Differential Revision: https://reviews.llvm.org/D113725
2022-02-22[NFC] Remove dead code (try 2)Arthur Eubanks1-7/+5
This is causing ../../llvm/include/llvm/Object/MachO.h:379:13: warning: private field 'Kind' is not used [-Wunused-private-field] FixupKind Kind; Previous attempt in a23f7c0cb6b42a06bc9707fdf46ce2a90080f61f.
2022-02-22Add support for chained fixup load commands to MachOObjectFileAdrian Prantl1-3/+158
This is part of a series of patches to upstream support for Mach-O chained fixups. This patch adds support for parsing the chained fixup load command and parsing the chained fixups header. It also puts into place the abstract interface that will be used to iterate over the fixups. Differential Revision: https://reviews.llvm.org/D113630
2022-02-13[ObjectYAML][MachO] Add LC_FUNCTION_STARTS supportKeith Smiley1-1/+15
This adds support for encoding and decoding the LC_FUNCTION_STARTS load command payload. Differential Revision: https://reviews.llvm.org/D119205
2022-02-10Cleanup LLVMObject headersserge-sans-paille1-1/+1
Most notably, llvm/Object/Binary.h no longer includes llvm/Support/MemoryBuffer.h llvm/Object/MachOUniversal*.h no longer include llvm/Object/Archive.h llvm/Object/TapiUniversal.h no longer includes llvm/Object/TapiFile.h llvm-project preprocessed size: before: 1068185081 after: 1068324320 Discourse thread: https://discourse.llvm.org/t/include-what-you-use-include-cleanup Differential Revision: https://reviews.llvm.org/D119457
2022-02-01Change namespace llvm::swift to namespace llvm::binaryformat because of ↵Shubham Sandeep Rastogi1-4/+5
clashes with the apple/llvm-project repository The namespace llvm::swift is causing errors to pop up in the apple/llvm-project build when cherry-picking 4ce1f3d47c33 into apple/llvm-project Differential Review: https://reviews.llvm.org/D118716
2022-01-28Emit swift5 reflection section data in dsym bundle generated by dsymutil in ↵Shubham Sandeep Rastogi1-0/+12
the Dwarf section. Add support for Swift reflection metadata to dsymutil. This patch adds support for copying Swift reflection metadata (__swift5_.* sections) from .o files to into the symbol-rich binary in the output .dSYM. The functionality is automatically enabled only if a .o file has reflection metadata sections and the binary doesn't. When copying dsymutil moves the section from the __TEXT segment to the __DWARF segment. rdar://76973336 Differential Revision: https://reviews.llvm.org/D115007
2022-01-26Revert "Emit swift5 reflection section data in dsym bundle generated by ↵Shubham Sandeep Rastogi1-12/+0
dsymutil in the Dwarf section." This reverts commit 50f50f2582993a079dbcfb8e7ba48920f41e6be0.
2022-01-26Emit swift5 reflection section data in dsym bundle generated by dsymutil in ↵Shubham Sandeep Rastogi1-0/+12
the Dwarf section. Add support for Swift reflection metadata to dsymutil. This patch adds support for copying Swift reflection metadata (__swift5_.* sections) from .o files to into the symbol-rich binary in the output .dSYM. The functionality is automatically enabled only if a .o file has reflection metadata sections and the binary doesn't. When copying dsymutil moves the section from the __TEXT segment to the __DWARF segment. rdar://76973336 Differential Revision: https://reviews.llvm.org/D115007
2022-01-26Revert "Rename llvm::array_lengthof into llvm::size to match std::size from ↵Benjamin Kramer1-1/+1
C++17" This reverts commit ef8206320769ad31422a803a0d6de6077fd231d2. - It conflicts with the existing llvm::size in STLExtras, which will now never be called. - Calling it without llvm:: breaks C++17 compat
2022-01-26Rename llvm::array_lengthof into llvm::size to match std::size from C++17serge-sans-paille1-1/+1
As a conquence move llvm::array_lengthof from STLExtras.h to STLForwardCompat.h (which is included by STLExtras.h so no build breakage expected).
2022-01-21Revert "Emit swift5 reflection section data in dsym bundle generated by ↵Shubham Sandeep Rastogi1-12/+0
dsymutil in the Dwarf section." This reverts commit d84d1135d80c1dead6564347943ba56eed5aac3b. to investigate buildbot failures
2022-01-21Emit swift5 reflection section data in dsym bundle generated by dsymutil in ↵Shubham Sandeep Rastogi1-0/+12
the Dwarf section. Add support for Swift reflection metadata to dsymutil. This patch adds support for copying Swift reflection metadata (__swift5_.* sections) from .o files to into the symbol-rich binary in the output .dSYM. The functionality is automatically enabled only if a .o file has reflection metadata sections and the binary doesn't. When copying dsymutil moves the section from the __TEXT segment to the __DWARF segment. rdar://76973336 https://reviews.llvm.org/D115007