aboutsummaryrefslogtreecommitdiff
path: root/llvm/lib/MC/MCAsmBackend.cpp
AgeCommit message (Collapse)AuthorFilesLines
2025-08-03MCSymbol: Replace isELF with MCContext::isELFFangrui Song1-3/+2
2025-07-15MC: Restructure MCFragment as a fixed part and a variable tailFangrui Song1-5/+4
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-06MCAsmBackend: Simplify FT_Data/FT_Relaxable codeFangrui Song1-15/+3
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-04MCFixup: Remove FK_PCRel_Fangrui Song1-4/+0
The generic FK_Data_ fixup kinds handle both absolute and PC-relative fixups. ELFObjectWriter sets IsPCRel to true for `.long foo-.`, so the backend has to handle PC-relative FK_Data_. However, the existence of FK_PCRel_ encouraged backends to implement it as a separate fixup type, leading to redundant and error-prone code. Removing FK_PCRel_ simplifies the overall fixup mechanism.
2025-07-04MCFixup: Remove FKF_IsPCRelFangrui Song1-4/+6
PC-relative fixups compute their values as `sym_a - current_location + offset` (S - P + A). Now that targets have set PCRel at fixup creation time, we can remove some overhead from MCAssembler::evaluateFixup.
2025-07-02MCAsmBackend: Merge addReloc into applyFixup (#146820)Fangrui Song1-7/+4
Follow-up to #141333. Relocation generation called both addReloc and applyFixup, with the default addReloc invoking shouldForceRelocation, resulting in three virtual calls. This approach was also inflexible, as targets needing additional data required extending `shouldForceRelocation` (see #73721, resolved by #141311). This change integrates relocation handling into applyFixup, eliminating two virtual calls. The prior default addReloc is renamed to maybeAddReloc. Targets overriding addReloc now call their customized addReloc implementation.
2025-05-24MC: Remove redundant relocations for label differencesFangrui Song1-3/+0
For the label difference A-B where A and B are in the same section, if the section contains no linker-relaxable instruction, we can disable the framnent walk code path (https://reviews.llvm.org/D155357), removing redundant relocations. This optimization is available since we now track per-section linker-relaxable instructions (#140692). lld/test/ELF/loongarch-reloc-leb128.s , introduced in #81133, has been updated in 9662a6039c0320eb4473d87b47f0ed891a0f111c to prevent coverage loss.
2025-05-24MCAsmBackend: Remove the MCAssembler argument from fixupNeedsRelaxationAdvancedFangrui Song1-2/+1
2025-05-24MC: Simplify recordRelocationFangrui Song1-1/+1
* 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-23MCAsmBackend: Remove the MCAssembler argument from addRelocFangrui Song1-4/+4
2025-05-23MCAsmBackend: Remove the MCAssembler argument from shouldForceRelocationFangrui Song1-1/+1
It is only required by ARM, which can now use the member variable.
2025-05-23MCAsmBackend: Simplify applyFixup (#141333)Fangrui Song1-0/+21
Remove the MCSubtargetInfo argument from applyFixup, introduced in https://reviews.llvm.org/D45962 , as it's only required by ARM. Instead, add const MCFragment & so that ARMAsmBackend can retrieve MCSubtargetInfo via a static member function. Additionally, remove the MCAssembler argument, which is also only required by ARM. Additionally, make applyReloc non-const. Its arguments now fully cover addReloc's functionality.
2025-05-23MCAsmBackend: Add member variable MCAssembler * and define getContextFangrui Song1-0/+2
A lot of member functions have the MCAssembler * argument just to call getContext. Let's cache the MCAssembler pointer.
2025-05-23MCAsmBackend: Remove MCSubtargetInfo argumentFangrui Song1-2/+1
After #141311 removed the MCSubtargetInfo argument from shouldForceRelocation, addReloc does not need this argument, either. In a rare scenario that the information is needed, the target should check the MCFragment subclass and get it from MCDataFragment/MCRelaxableFragment.
2025-05-23[MC] Don't pass MCSubtargetInfo down to shouldForceRelocation and ↵Fangrui Song1-1/+1
evaluateTargetFixup (#141311) This reverts the code change in commit e87f33d9ce785668223c3bcc4e06956985cccda1 (#73721) but keeps its test. There have been many changes to lib/MC and AsmBackend.cpp files, so this is not a pure revert. #73721, a workaround to generate necessary relocations in mixed non-relax/relax code, is no longer necessary after #140692 fixed the root issue (whether two locations are separated by a fragment with indeterminate size due to linker relaxation).
2025-05-22[MC] Restore MCAsmBackend::shouldForceRelocation to falseFangrui Song1-6/+0
For IsPCRel fixups, we had the `A->getKind() != MCSymbolRefExpr::VK_None` condition to force relocations. The condition has then been changed to `Target.getSpecifier()` (086af836889436baffc71c743c7c8259bad8ed60). 38c3ad36be1facbe6db2dede7e93c0f12fb4e1dc updated shouldForceRelocation to test `Target.getSpecifier`. It is not a good fit as many targets with %lo/%hi style specifiers (SPARC, MIPS, PowerPC, RISC-V) do not force relocations for these specifiers. Revert the Target.getSpecifier implementation (38c3ad36be1facbe6db2dede7e93c0f12fb4e1dc) and update targets that need it (SystemZAsmBackend/X86AsmBackend) instead. Targets need customization might define addReloc instead. Note: The X86AsmBackend implementation is too conservative. GNU Assembler doesn't emit a relocation for `call local@plt; local` a3d39316764726ed9fd939550c7781199b1eb77e added missing coverage for GOT/PLT related relocations.
2025-05-22Revert "[MC] Restore MCAsmBackend::shouldForceRelocation to false"JP Lehr1-0/+6
This reverts commit 6f6dc1f239433393c0b09430193beb85d03464c8. Resulted in several bot failures.
2025-05-21[MC] Restore MCAsmBackend::shouldForceRelocation to falseFangrui Song1-6/+0
Revert the Target.getSpecifier implementation (38c3ad36be1facbe6db2dede7e93c0f12fb4e1dc) and update SystemZAsmBackend instead. Many targets with %lo/%hi style specifiers (SPARC, MIPS, PowerPC, RISC-V) do not force relocations for these specifiers. Additionally, with the introduction of the addReloc hook, shouldForceRelocation is not that necessary and should probably be phased out.
2025-05-19RISCV,LoongArch: Encode RELAX relocation implicitlyFangrui Song1-2/+2
When linker relaxation is enabled, relaxable relocations are followed by a R_RISCV_RELAX/R_LARCH_RELAX relocation. They are encoded as two fixups by CodeEmitter and expected to have the same `IsResolved` value within MCAssembler::evaluateFixup (they must lead to either 0 or 2 relocations). This scheme wasite space and requires RISCVAsmBackend::shouldForceRelocation to be conservative. This patch introduces MCFixup::NeedsRelax to encode the RELAX relocation implicitly. The fixup will lead to either 0 or 2 relocations. Pull Request: https://github.com/llvm/llvm-project/pull/140494
2025-05-18MC: Generalize RISCV/LoongArch handleAddSubRelocations and AVR ↵Fangrui Song1-0/+11
shouldForceRelocation Introduce MCAsmBackend::addReloc to manage relocation appending. The default implementation uses shouldForceRelocation to check unresolved fixups and calls recordRelocation to append a relocation when needed. RISCV and LoongArch override addReloc to handle ADD/SUB relocations, with potential support for RELAX relocations in the future. AVR overrides addReloc to customize shouldForceRelocation behavior (#121498). applyFixup is moved into evaluateFixup.
2025-04-18MCFixup: Make FixupKindInfo smaller and change getFixupKindInfo to return valueFangrui Song1-1/+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-18MCFixup: Move relocation values before FK_NONEFangrui Song1-2/+2
Simplify the process of encoding a raw relocation type using MCFixupKind. Currently, FirstRelocationkind is utilized by AArch64, LoongArch, and RISCV.
2025-04-13MCAsmBackend,Hexagon: Remove MCRelaxableFragment from ↵Fangrui Song1-1/+0
fixupNeedsRelaxationAdvanced Among fixupNeedsRelaxationAdvanced (introduced by https://reviews.llvm.org/D8217) targets, only Hexagon needs the `MCRelaxableFragment` parameter (commit 86f218e7ec5d941b7785eaebcb8f4cad76db8a64) to get the instruction packet (MCInst with sub-instruction operands). As fixupNeedsRelaxationAdvanced follows mayNeedRelaxation, we can store the MCInst in mayNeedRelaxation and eliminate the MCRelaxableFragment parameter. Follow-up to 7c83b7ef1796210451b839f4c58f2815f4aedfe5 that eliminates the MCRelaxableFragment parameter from fixupNeedsRelaxation.
2025-04-13[MC] Refactor fixup evaluation and relocation generationFangrui Song1-3/+5
Follow-up to commits 5710759eb390c0d5274c2a4d43967282d7df1993 and 634f9a981571eae000c1adc311014c5c64486187 - Integrate `evaluateFixup` into `recordRelocation` and inline code within `MCAssembler::layout`, removing `handleFixup`. - Update `fixupNeedsRelaxation` to bypass `shouldForceRelocation` when calling `evaluateFixup`, eliminating the `WasForced` workaround for RISC-V linker relaxation (https://reviews.llvm.org/D46350 ).
2025-04-13MCAsmBackend,X86: Pass MCValue to fixupNeedsRelaxationAdvanced. NFCFangrui Song1-5/+3
This parameter eliminates a redundant computation for VK_ABS8 in X86 and reduces reliance on shouldForceRelocation in relaxation decisions. Note: `local: jmp local@plt` relaxes JMP. This behavior depends on fixupNeedsRelaxation calling shouldForceRelocation, which might change in the future.
2025-04-05Define MCAsmBackend::shouldForceRelocationFangrui Song1-0/+6
Return true if the MCValue has a specifier. When a relocation specifier is specified, GNU Assembler will generate a relocation unless the specifier can be optimized due to target-specific reasons (e.g. PPC `@l` `@ha`). This reduces targets' reliance on a MCAssembler::evaluateFixup hack `if (Target.SymSpecifier || SA.isUndefined()) {`, previosuly `if (A->getKind() != MCSymbolRefExpr::VK_None || SA.isUndefined()) {` llvm/test/MC/SystemZ/fixups.s is known to rely on this hack.
2025-03-02[MC] Move MIPS-specific gprel/tprel/dtprel from MCStreamer to MipsTargetStreamerFangrui Song1-8/+0
https://reviews.llvm.org/D23669 inappropriately added MIPS-specific dtprel/tprel directives to MCStreamer. In addition, llvm-mc -filetype=null parsing these directives will crash. This patch moves these functions to MipsTargetStreamer and fixes -filetype=null. gprel32 and gprel64, called by AsmPrinter, are moved to MCTargetStreamer.
2024-11-01[MC] Export MCDXContainerObjectWriterFangrui Song1-1/+1
Similar to other ObjectWriter classes.
2024-07-24MC: Inline createMachObjectWriter into MCAsmBackendFangrui Song1-2/+2
We could do the same to COFF once WinCOFFObjectWriter is cleaned up (#100303).
2024-07-23ELFObjectWriter: Remove unneeded subclassesFangrui Song1-5/+6
2024-07-01[MC] Remove two unused parameters from MCAsmBackend::fixupNeedsRelaxationFangrui Song1-1/+1
fixupNeedsRelaxation is a simple implementation for fixupNeedsRelaxationAdvanced. Its users do not utilize MCAsmLayout or MCRelaxableFragment. Follow-up to 22c7317f1e954b34a46640db5d509bae1c633348 ("[MC] Remove the MCAsmLayout parameter from relocation related functions").
2024-07-01[MC] Remove the MCAsmLayout parameter from relocation related functionsFangrui Song1-5/+7
2023-11-09[RISCV] Support R_RISCV_SET_ULEB128/R_RISCV_SUB_ULEB128 for .uleb128 directivesFangrui Song1-0/+1
For a label difference like `.uleb128 A-B`, MC folds A-B even if A and B are separated by a RISC-V linker-relaxable instruction. This incorrect behavior is currently abused by DWARF v5 .debug_loclists/.debug_rnglists (DW_LLE_offset_pair/DW_RLE_offset_pair entry kinds) implemented in Clang/LLVM (see https://github.com/ClangBuiltLinux/linux/issues/1719 for an instance). https://github.com/riscv-non-isa/riscv-elf-psabi-doc/commit/96d6e190e9fc04a8517f9ff7fb9aed3e9876cbd6 defined R_RISCV_SET_ULEB128/R_RISCV_SUB_ULEB128. This patch generates such a pair of relocations to represent A-B that should not be folded. GNU assembler computes the directive size by ignoring shrinkable section content, therefore after linking the value of A-B cannot use more bytes than the reserved number (`final size of uleb128 value at offset ... exceeds available space`). We make the same assumption. ``` w1: call foo w2: .space 120 w3: .uleb128 w2-w1 # 1 byte, 0x08 .uleb128 w3-w1 # 2 bytes, 0x80 0x01 ``` We do not conservatively reserve 10 bytes (maximum size of an uleb128 for uint64_t) as that would pessimize DWARF v5 DW_LLE_offset_pair/DW_RLE_offset_pair, nullifying the benefits of introducing R_RISCV_SET_ULEB128/R_RISCV_SUB_ULEB128 relocations. The supported expressions are limited. For example, * non-subtraction `.uleb128 A` is not allowed * `.uleb128 A-B`: report an error unless A and B are both defined and in the same section The new cl::opt `-riscv-uleb128-reloc` can be used to suppress the relocations. Reviewed By: asb Differential Revision: https://reviews.llvm.org/D157657
2023-10-12Use llvm::endianness::{big,little,native} (NFC)Kazu Hirata1-4/+4
Note that llvm::support::endianness has been renamed to llvm::endianness while becoming an enum class as opposed to an enum. This patch replaces support::{big,little,native} with llvm::endianness::{big,little,native}.
2023-10-10Use llvm::endianness (NFC)Kazu Hirata1-1/+1
Now that llvm::support::endianness has been renamed to llvm::endianness, we can use the shorter form. This patch replaces support::endianness with llvm::endianness.
2023-09-29[SystemZ/zOS/GOFF] Implement GOFF writer for empty files.Kai Nacke1-0/+4
Set ups the infrastructure to create an empty GOFF file. Also adds a GOFF writer which writes only HDR/END records. Reviewed By: jhenderson, kpn Differential Revision: https://reviews.llvm.org/D111437
2023-08-09[MC] Remove FK_Data_6bFangrui Song1-1/+0
D58335 introduced FK_Data_6b for emitting R_RISCV_SET6/R_RISCV_SUB6 in .eh_frame/.debug_frame. This is no longer needed after commit c8ed138c34ddb22610f18468c1b7938f9e2abae5 removed unneeded fixup kinds for R_RISCV_{SET,ADD,SUB}* and getKindForSizeInBits.
2023-06-29[RISCV] Make linker-relaxable instructions terminate MCDataFragmentFangrui Song1-1/+2
`MCExpr::evaluateAsAbsolute` has a longstanding bug. When the MCAssembler is non-null and the MCAsmLayout is null, it may incorrectly fold A-B even if A and B are separated by a linker-relaxable instruction. This behavior can suppress some ADD/SUB relocations and lead to wrong results if the linker performs relaxation. To fix the bug, ensure that linker-relaxable instructions only appear at the end of an MCDataFragment, thereby making them terminate the fragment. When computing A-B, suppress folding if A and B are separated by a linker-relaxable instruction. * `.subsection` now correctly give errors for non-foldable expressions. * gen-dwarf.s will pass even if we add back the .debug_line or .eh_frame/.debug_frame code from D150004 * This will fix suppressed relocation when we add R_RISCV_SET_ULEB128/R_RISCV_SUB_ULEB128. In the future, we should investigate the desired behavior for `MCExpr::evaluateAsAbsolute` when both MCAssembler and MCAsmLayout are non-null. (Note: MCRelaxableFragment is only for assembler-relaxation. If we ever need linker-relaxable MCRelaxableFragment, we would need to adjust RISCVMCExpr.cpp (D58943/D73211).) Depends on D153096 Differential Revision: https://reviews.llvm.org/D153097
2023-06-26Reland [COFF] Support -gsplit-dwarf for COFF on WindowsHaohai Wen1-1/+4
This relands 3eee5aa528abd67bb6d057e25ce1980d0d38c445 with fixes.
2023-06-25Revert "[COFF] Support -gsplit-dwarf for COFF on Windows"Nico Weber1-4/+1
This reverts commit 3eee5aa528abd67bb6d057e25ce1980d0d38c445. Breaks tests on mac, see https://reviews.llvm.org/D152785#4447118
2023-06-25[COFF] Support -gsplit-dwarf for COFF on WindowsHaohai Wen1-1/+4
D152340 has split WinCOFFObjectWriter to WinCOFFWriter. This patch adds another WinCOFFWriter as DwoWriter to write Dwo sections to dwo file. Driver options are also updated accordingly to support -gsplit-dwarf in CL mode. e.g. $ clang-cl -c -gdwarf -gsplit-dwarf foo.c Like what -gsplit-dwarf did in ELF, using this option will create DWARF object (.dwo) file. DWARF debug info is split between COFF object and DWARF object file. It can reduce the executable file size especially for large project. Reviewed By: skan, MaskRay Differential Revision: https://reviews.llvm.org/D152785
2023-06-12[MC][MachO]Do not emit DWARF for no-personality caseoontvoo1-8/+13
Detail: Follow up to D144999, where we emitted DWARF for non-canonical personality. Reviewed By: jyknight Differential Revision: https://reviews.llvm.org/D152540
2023-06-07Reland "D144999 [MC][MachO]Only emits compact-unwind format for "canonical" ↵Vy Nguyen1-0/+11
personality symbols. For the rest, use DWARFs." Reasons for rolling forward: - the crash reported from Chromium was fixed in D151824 (not related to this patch at all) - since D152824 was committed, it should now be safe to roll this forward. New change: - add an additional _ in name check This reverts commit 4980eead4d0b4666d53dad07afb091375b3a13a0.
2023-05-19Revert "[RFC][MC][MachO]Only emits compact-unwind format for "canonical" ↵Nico Weber1-11/+0
personality symbols. For the rest, use DWARFs." This reverts commit 09aaf53a05e3786eea374f3ce57574225036412d. Causes toolchain asserts building libc++ for x86_64, see https://reviews.llvm.org/D144999#4356215
2023-05-18[RFC][MC][MachO]Only emits compact-unwind format for "canonical" personality ↵Vy Nguyen1-0/+11
symbols. For the rest, use DWARFs. Details: https://github.com/rust-lang/rust/issues/102754 The MachO format uses 2 bits to encode these personality funtions, with 0 reserved for "no-personality". This means we can only have up to 3 personality. There are already three popular personalities: __gxx_personality_v0, __gcc_personality_v0, and __objc_personality_v0. As a result, any system that needs custom-personality will run into a problem. This patch implemented jyknight's proposal to simply force DWARFs for all non-canonical personality functions. Differential Revision: https://reviews.llvm.org/D144999
2022-12-06[ADT] Don't including None.h (NFC)Kazu Hirata1-1/+0
These source files no longer use None, so they do not 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-04[MC] llvm::Optional => std::optionalFangrui Song1-1/+1
https://discourse.llvm.org/t/deprecating-llvm-optional-x-hasvalue-getvalue-getvalueor/63716
2022-12-02[llvm] Use std::nullopt instead of None (NFC)Kazu Hirata1-1/+1
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-09-09[llvm] Remove includes of `llvm/Support/STLArrayExtras.h`Joe Loser1-1/+0
`llvm` and downstream internal callers no longer use `array_lengthof`, so drop the include everywhere. Differential Revision: https://reviews.llvm.org/D133600