aboutsummaryrefslogtreecommitdiff
path: root/llvm/lib/MC/MachObjectWriter.cpp
AgeCommit message (Collapse)AuthorFilesLines
2025-08-22MCSymbol: Avoid isExported/setExportedFangrui Song1-7/+9
The next change will move these methods from the base class.
2025-08-03MCSymbolMachO: Remove classofFangrui Song1-3/+3
The object file format specific derived classes are used in context where the type is statically known. We don't use isa/dyn_cast and we want to eliminate MCSymbol::Kind in the base class.
2025-08-03MCSymbolMachO: Migrate away from classofFangrui Song1-9/+10
The object file format specific derived classes are used in context where the type is statically known. We don't use isa/dyn_cast and we want to eliminate MCSymbol::Kind in the base class.
2025-07-28MCFragment: Migrate away from appendContentsFangrui Song1-1/+1
The fixed-size content of the MCFragment object will be stored as trailing data (#150846). Any post-assembler-layout adjustments must target the variable-size tail.
2025-07-26MCSectionCOFF: Avoid castFangrui Song1-17/+15
The object file format specific derived classes are used in context like MCStreamer and MCObjectTargetWriter where the type is statically known. We don't use isa/dyn_cast and we want to eliminate MCSection::SectionVariant in the base class.
2025-07-20MCFragment: Remove clearContents and uses of non-streaming doneAppendingFangrui Song1-6/+5
Make the fixed-size part of MCFragment append-only to support allocating content as trailing data. The `doneAppending` API is reserved by MCStreamer API before finish and should not be used by the addrsig and call-graph-profile features.
2025-07-20MC: Rename isVirtualSection to isBssSectionFangrui Song1-6/+6
The term BSS (Block Started by Symbol) is a standard, widely recognized term, available in the a.out object file format and adopted by formats like COFF, XCOFF, Mach-O (called S_ZEROFILL while `__bss` is also used), and ELF. To avoid introducing unfamiliar terms, we should use isBSSSection instead of isVirtualSection.
2025-07-15MC: Restructure MCFragment as a fixed part and a variable tailFangrui Song1-1/+1
Refactor the fragment representation of `push rax; jmp foo; nop; jmp foo`, previously encoded as `MCDataFragment(nop); MCRelaxableFragment(jmp foo); MCDataFragment(nop); MCRelaxableFragment(jmp foo)`, to ``` MCFragment(fixed: push rax, variable: jmp foo) MCFragment(fixed: nop, variable: jmp foo) ``` Changes: * Eliminate MCEncodedFragment, moving content and fixup storage to MCFragment. * The new MCFragment contains a fixed-size content (similar to previous MCDataFragment) and an optional variable-size tail. * The variable-size tail supports FT_Relaxable, FT_LEB, FT_Dwarf, and FT_DwarfFrame, with plans to extend to other fragment types. dyn_cast/isa should be avoided for the converted fragment subclasses. * In `setVarFixups`, source fixup offsets are relative to the variable part's start. Stored fixup (in `FixupStorage`) offsets are relative to the fixed part's start. A lot of code does `getFragmentOffset(Frag) + Fixup.getOffset()`, expecting the fixup offset to be relative to the fixed part's start. * HexagonAsmBackend::fixupNeedsRelaxationAdvanced needs to know the associated instruction for a fixup. We have to add a `const MCFragment &` parameter. * In MCObjectStreamer, extend `absoluteSymbolDiff` to apply to FT_Relaxable as otherwise there would be many more FT_DwarfFrame fragments in -g compilations. https://llvm-compile-time-tracker.com/compare.php?from=28e1473e8e523150914e8c7ea50b44fb0d2a8d65&to=778d68ad1d48e7f111ea853dd249912c601bee89&stat=instructions:u ``` stage2-O0-g instructins:u geomeon (-0.07%) stage1-ReleaseLTO-g (link only) max-rss geomean (-0.39%) ``` ``` % /t/clang-old -g -c sqlite3.i -w -mllvm -debug-only=mc-dump &| awk '/^[0-9]+/{s[$2]++;tot++} END{print "Total",tot; n=asorti(s, si); for(i=1;i<=n;i++) print si[i],s[si[i]]}' Total 59675 Align 2215 Data 29700 Dwarf 12044 DwarfCallFrame 4216 Fill 92 LEB 12 Relaxable 11396 % /t/clang-new -g -c sqlite3.i -w -mllvm -debug-only=mc-dump &| awk '/^[0-9]+/{s[$2]++;tot++} END{print "Total",tot; n=asorti(s, si); for(i=1;i<=n;i++) print si[i],s[si[i]]}' Total 32287 Align 2215 Data 2312 Dwarf 12044 DwarfCallFrame 4216 Fill 92 LEB 12 Relaxable 11396 ``` Pull Request: https://github.com/llvm/llvm-project/pull/148544
2025-07-05MC: Remove llvm/MC/MCFixupKindInfo.hFangrui Song1-1/+0
The file used to define `MCFixupKindInfo`, a simple structure, which is now in MCAsmBackend.h.
2025-07-03MCAsmBackend: Replace FKF_IsPCRel with isPCRel()Fangrui Song1-6/+0
2025-07-01MC: Store fragment content and fixups out-of-lineFangrui Song1-2/+3
Moved `Contents` and `Fixups` SmallVector storage to MCSection, enabling trivial destructors for most fragment subclasses and eliminating the need for MCFragment::destroy in ~MCSection. For appending content to the current section, use getContentsForAppending. During assembler relaxation, prefer setContents/setFixups, which may involve copying and reduce the benefits of https://reviews.llvm.org/D145791. Moving only Contents out-of-line caused a slight performance regression (Alexis Engelke's 2024 prototype). By also moving Fragments out-of-line, fragment destructors become trivial, resulting in neglgible instructions:u increase for "stage2-O0-g" and [large max-rss decrease](https://llvm-compile-time-tracker.com/compare.php?from=84e82746c3ff63ec23a8b85e9efd4f7fccf92590&to=555a28c0b2f8250a9cf86fd267a04b0460283e15&stat=max-rss&linkStats=on) for the "stage1-ReleaseLTO-g (link only)" benchmark. ( An older version using fewer inline functions: https://llvm-compile-time-tracker.com/compare.php?from=bb982e733cfcda7e4cfb0583544f68af65211ed1&to=f12d55f97c47717d438951ecddecf8ebd28c296b&linkStats=on ) Now using plain SmallVector in MCSection for storage, with potential for future allocator optimizations, such as allocating `Contents` as the trailing object of MCDataFragment. (GNU Assembler uses gnulib's obstack for fragment management.) Co-authored-by: Alexis Engelke <engelke@in.tum.de> Pull Request: https://github.com/llvm/llvm-project/pull/146307
2025-06-30MC: Merge MCFragment.h into MCSection.hFangrui Song1-1/+0
... due to their close relationship. MCSection's inline functions (e.g. iterator) access MCFragment, and we want MCFragment's inline functions to access MCSection similarly (#146307). Pull Request: https://github.com/llvm/llvm-project/pull/146315
2025-05-25MachObjectWriter: Simplify Asm.getContext().reportErrorFangrui Song1-0/+5
Similar to b65760bc7fcdee8179bf1e57fce3786737528dd8 for ELF.
2025-05-25MachObjectWriter: Remove the MCAssembler argument from getSymbolAddressFangrui Song1-9/+8
2025-05-24MCObjectWriter: Remove the MCAssembler argument from writeObjectFangrui Song1-1/+2
2025-05-24MC: Simplify recordRelocationFangrui Song1-3/+2
* Remove the MCAssembler * argument. Change subclasses to use MCAssembler *MCObjectWriter::Asm. * Remove pure specifier and add an empty implementation * Change MCFragment * to MCFragment &
2025-05-24MCObjectwriter: Add getContext and simplify codeFangrui Song1-6/+6
2025-05-24MCObjectwriter: Add member variable MCAssembler * and simplify codeFangrui Song1-5/+5
2025-04-18MCFixup: Make FixupKindInfo smaller and change getFixupKindInfo to return valueFangrui Song1-2/+1
We will increase the use of raw relocation types and eliminate fixup kinds that correspond to relocation types. The getFixupKindInfo functions will return an rvalue instead. Let's update the return type from a const reference to a value type.
2025-04-06[MC,MachO] Replace SectionAddrMap workaround with cleaner variable handlingFangrui Song1-1/+1
Mach-O's ARM and X86 writers use MCExpr's `SectionAddrMap *Addrs` argument to compute label differences, which was a bit of a hack. The AArch64MachObjectWriter does this better by using `getSymbolAddress` in its `recordRelocation` function. This commit: 1. Moves the `SectionAddrMap` logic into the Mach-O code, removing the workaround. 2. Fixes a bug in `MachObjectWriter::getSymbolAddress` where it failed to subtract the `SymB` value. This bug has been present since commit b200f93125eb019d69c220fa447faea4f5d4eb8a (2011).
2025-04-05[MC] Replace getSymA()->getSymbol() with getAddSym. NFCFangrui Song1-3/+3
We will replace the MCSymbolRefExpr member in MCValue with MCSymbol. This change reduces dependence on MCSymbolRefExpr.
2025-04-05[MC] Replace getSymA()->getSymbol() with getAddSym. NFCFangrui Song1-3/+3
We will replace the MCSymbolRefExpr member in MCValue with MCSymbol. This change reduces dependence on MCSymbolRefExpr.
2025-03-23MCValue: Simplify code with getSubSymFangrui Song1-5/+5
MCValue::SymB is a MCSymbolRefExpr *, which might become MCSymbol * in the future. Simplify some code that uses MCValue::SymB.
2025-03-15[MC] evaluateAsRelocatableImpl: remove the Fixup argumentFangrui Song1-1/+1
Follow-up to d6fbffa23c84e622735b3e880fd800985c1c0072 . This commit updates all call sites and removes the argument from the function.
2024-11-15[MC] Remove unused includes (NFC) (#116317)Kazu Hirata1-2/+0
Identified with misc-include-cleaner.
2024-08-20[AArch64][MachO] Add ptrauth ABI version to arm64e cpusubtype. (#104650)Ahmed Bougacha1-1/+12
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-07-24MC: Inline createMachObjectWriter into MCAsmBackendFangrui Song1-7/+0
We could do the same to COFF once WinCOFFObjectWriter is cleaned up (#100303).
2024-07-22MCAssembler: Move SubsectionsViaSymbols; to MCObjectWriterFangrui Song1-2/+2
2024-07-22MCAssembler: Move LinkerOptions to MachObjectWriterFangrui Song1-2/+3
2024-07-22MCAssembler: Move CGProfile to MCObjectWriterFangrui Song1-2/+2
2024-07-21[MC] Move VersionInfo to MachObjectWriterFangrui Song1-5/+5
2024-07-21[MC] Move LOHContainer to MachObjectwriterFangrui Song1-2/+3
2024-07-21[MC] Move isPrivateExtern to MCSymbolMachOFangrui Song1-1/+1
2024-07-17[MachO] Detect overflow in section offset. (#98685)Eli Friedman1-1/+17
The section offset field is only 32 bits; if the computed section offset is larger, make sure we don't emit a corrupt object file.
2024-07-05MCAssembler: Remove unneeded non-const iterators for Sections and misleading ↵Fangrui Song1-4/+3
size() The pointers cannot be mutated even if the dereferenced MCSection can.
2024-07-04[MC] Move MCAssembler::DataRegions to MachObjectWriterFangrui Song1-14/+11
and make some cleanup.
2024-07-04[MC] Move MCAssembler::IndirectSymbols to MachObjectWriterFangrui Song1-5/+6
2024-07-04[MC,MachO] Simplify IndirectSybolsFangrui Song1-23/+16
2024-07-03[MC] Move MCAssembler::isSymbolLinkerVisible to MCSymbolMachOFangrui Song1-3/+3
2024-07-02Move MCSection::LayoutOrder to MCSectionMachOFangrui Song1-3/+3
This variable is similar to `Ordinal` but only used for Mach-O to place zerofill sections ("virtual sections" in MC term) after non-zerofill ones. Follow-up to 7840c0066837797cdeb62aab63044b964aa7f372. Pull Request: https://github.com/llvm/llvm-project/pull/97474
2024-07-01MachObjectWriter: replace the MCAsmLayout parameter with MCAssemblerFangrui Song1-16/+12
2024-07-01MCExpr::evaluateAsRelocatable: replace the MCAsmLayout parameter with ↵Fangrui Song1-1/+2
MCAssembler Continue the MCAsmLayout removal work started by 67957a45ee1ec42ae1671cdbfa0d73127346cc95.
2024-07-01MachObjectWrite::reset: clear SectionAddress and SectionOrderFangrui Song1-0/+2
Otherwise llvm/test/MC/MachO/empty-twice.ll might fail. Fixes: 7840c0066837797cdeb62aab63044b964aa7f372 ("[MC] Move MCAsmLayout::SectionOrder to MachObjectWriter::SectionOrder")
2024-07-01[MC] Move MCAsmLayout::SectionOrder to MachObjectWriter::SectionOrderFangrui Song1-3/+19
Follow-up to 2c1fb411ce3aed148a278660d215e0f88ff9b9be. SectionOrder is Mach-O specific to place zerofill sections after non-zerofill sections in the object writer.
2024-07-01[MC] Remove MCAsmLayout::{getSymbolOffset,getBaseSymbol}Fangrui Song1-1/+1
The MCAsmLayout::* forwarders added by 67957a45ee1ec42ae1671cdbfa0d73127346cc95 have all been removed.
2024-07-01[MC] Remove MCAsmLayout::{getSectionFileSize,getSectionAddressSize}Fangrui Song1-12/+12
2024-07-01[MC] Remove MCAsmLayout::getFragmentAddressFangrui Song1-3/+4
2024-07-01[MC] Remove the MCAsmLayout parameter from ↵Fangrui Song1-8/+6
MCObjectWriter::executePostLayoutBinding
2024-07-01[MC] Remove the MCAsmLayout parameter from ↵Fangrui Song1-8/+7
MCObjectWriter::{writeObject,writeSectionData}
2024-06-30[MC] Remove the MCAsmLayout parameter from MCObjectWriter::recordRelocationFangrui Song1-1/+0