aboutsummaryrefslogtreecommitdiff
path: root/llvm/lib/MC/MCContext.cpp
AgeCommit message (Collapse)AuthorFilesLines
14 daysMC: Better handle backslash-escaped symbols (#158780)Fangrui Song1-21/+28
The MCContext::getOrCreateSymbol change in #138817 was a workaround. With #158106, we can replace `getOrCreateSymbol` with `parseSymbol`, in llvm/lib/MC/MCParser to handle backslash-escaped symbols.
2025-08-22MCSymbol: Move isExported/setExported to MCSymbol{COFF,MachO,Wasm}Fangrui Song1-1/+0
Make it clear that other object file formats (e.g. ELF) do not use this field.
2025-08-17[llvm] Remove unused includes (NFC) (#154051)Kazu Hirata1-1/+0
These are identified by misc-include-cleaner. I've filtered out those that break builds. Also, I'm staying away from llvm-config.h, config.h, and Compiler.h, which likely cause platform- or compiler-specific build failures.
2025-08-03MCSymbol: Remove KindFangrui Song1-4/+2
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 discourage dynamic dispatching on the type.
2025-08-03MCSymbolXCOFF: Migrate away from classofFangrui Song1-2/+2
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-03MCSymbolWasm: Remove classofFangrui Song1-2/+2
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-03MCSymbolELF: Migrate away from classofFangrui Song1-2/+2
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-03MCSymbolELF: Migrate away from classofFangrui Song1-4/+7
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-03Move FragmentAllocator from MCContext to MCObjectStreamerFangrui Song1-3/+0
In MCContext::reset, delete a stale comment as MCCodeView no longer owns or deallocates MCFragment.
2025-07-26MC: Allocate initial fragment and define section symbol in changeSectionFangrui Song1-41/+9
Reland #150574 with a MCStreamer::changeSection change: In Mach-O, DWARF sections use Begin as a temporary label, requiring a label definition, unlike section symbols in other file formats. (Tested by dec978036ef1037753e7de5b78c978e71c49217b) --- 13a79bbfe583e1d8cc85d241b580907260065eb8 (2017) introduced fragment creation in MCContext for createELFSectionImpl, which was inappropriate. Fragments should only be created when using MCSteramer, not during `MCContext::get*Section` calls. `initMachOMCObjectFileInfo` defines multiple sections, some of which may not be used by the code generator. This caused symbol names matching these sections to be incorrectly marked as undefined (see https://reviews.llvm.org/D55173). The fragment code was later replicated in other file formats, such as WebAssembly (see https://reviews.llvm.org/D46561), XCOFF, and GOFF. This patch fixes the problem by moving initial fragment allocation from MCContext::createSection to MCStreamer::changeSection. While MCContext still creates a section symbol, the symbol is not attached to the initial fragment. In addition, * Move `emitLabel`/`setFragment` from `switchSection*` and overridden changeSection to `MCObjectStreamer::changeSection` for consistency. * De-virtualize `switchSectionNoPrint`. * test/CodeGen/XCore/section-name.ll now passes. XCore doesn't support MCObjectStreamer. I don't think the MCAsmStreamer output behavior change matters. Pull Request: https://github.com/llvm/llvm-project/pull/150574
2025-07-25MCSectionCOFF: Remove classofFangrui Song1-2/+2
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-25Revert "MC: Allocate initial fragment and define section symbol in ↵dyung1-9/+41
changeSection" (#150736) Reverts llvm/llvm-project#150574 This is causing a test failure on AArch64 MacOS bots: https://lab.llvm.org/buildbot/#/builders/190/builds/24187
2025-07-24MC: Allocate initial fragment and define section symbol in changeSectionFangrui Song1-41/+9
13a79bbfe583e1d8cc85d241b580907260065eb8 (2017) introduced fragment creation in MCContext for createELFSectionImpl, which was inappropriate. Fragments should only be created when using MCSteramer, not during `MCContext::get*Section` calls. `initMachOMCObjectFileInfo` defines multiple sections, some of which may not be used by the code generator. This caused symbol names matching these sections to be incorrectly marked as undefined (see https://reviews.llvm.org/D55173). The fragment code was later replicated in other file formats, such as WebAssembly (see https://reviews.llvm.org/D46561), XCOFF, and GOFF. This patch fixes the problem by moving initial fragment allocation from MCContext::createSection to MCStreamer::changeSection. While MCContext still creates a section symbol, the symbol is not attached to the initial fragment. In addition, move `emitLabel`/`setFragment` from `switchSection*` and overridden changeSection to `MCObjectStreamer::changeSection` for consistency. * test/CodeGen/XCore/section-name.ll now passes. XCore doesn't support MCObjectStreamer. I don't think the MCAsmStreamer output behavior change matters. Pull Request: https://github.com/llvm/llvm-project/pull/150574
2025-07-17[llvm] Use *Map::try_emplace (NFC) (#149257)Kazu Hirata1-3/+2
- try_emplace(Key) is shorter than insert({Key, nullptr}). - try_emplace performs value initialization without value parameters. - We overwrite values on successful insertion anyway. While we are at it, this patch simplifies the code with structured binding.
2025-07-15MC: Restructure MCFragment as a fixed part and a variable tailFangrui Song1-3/+3
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-15MC: Use reportFatalUsageError for COFF with non-windows (#147911)Matt Arsenault1-3/+4
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-06-29MC: Make save-temp-labels imply UseNamesOnTempLabelsFangrui Song1-0/+2
UseNamesOnTempLabels was false in MCObjectStreamer. `createTempSymbol` created symbols were unnamed, making debugging difficult.
2025-06-26[GOFF] Add writing of text records (#137235)Kai Nacke1-6/+12
Sections which are not allowed to carry data are marked as virtual. Only complication when writing out the text is that it must be written in chunks of 32k-1 bytes, which is done by having a wrapper stream writing those records. Data of BSS sections is not written, since the contents is known to be zero. Instead, the fill byte value is used.
2025-06-26[GOFF] Add writing of section symbols (#133799)Kai Nacke1-8/+33
Unlike other formats, the GOFF object file format uses a 2 dimensional structure to define the location of data. For example, the equivalent of the ELF .text section is made up of a Section Definition (SD) and a class (Element Definition; ED). The name of the SD symbol depends on the application, while the class has the predefined name C_CODE/C_CODE64 in AMODE31 and AMODE64 respectively. Data can be placed into this structure in 2 ways. First, the data (in a text record) can be associated with an ED symbol. To refer to data, a Label Definition (LD) is used to give an offset into the data a name. When binding, the whole data is pulled into the resulting executable, and the addresses given by the LD symbols are resolved. The alternative is to use a Part Definition (PR). In this case, the data (in a text record) is associated with the part. When binding, only the data of referenced PRs is pulled into the resulting binary. Both approaches are used. SD, ED, and PR elements are modeled by nested MCSectionGOFF instances, while LD elements are associated with MCSymbolGOFF instances. At the binary level, a record called "External Symbol Definition" (ESD) is used. The ESD has a type (SD, ED, PR, LD), and depending on the type a different subset of the fields is used.
2025-06-05[MC] Remove dead code. (#114798)Ivan Kosarev1-3/+0
2025-06-01MCContext::reset: clear RelSecnames & MacroMapFangrui Song1-0/+2
When MCContext is used for the second compile of llvm/test/MC/ELF/twice.ll, ensure that .rel.text and .rel.eh_frame strings do not come from the previous compilation copies.
2025-05-26MC: Allow .set to reassign non-MCConstantExpr expressionsFangrui Song1-0/+29
GNU Assembler supports symbol reassignment via .set, .equ, or =. However, LLVM's integrated assembler only allows reassignment for MCConstantExpr cases, as it struggles with scenarios like: ``` .data .set x, 0 .long x // reference the first instance x = .-.data .long x // reference the second instance .set x,.-.data .long x // reference the third instance ``` Between two assignments binds, we cannot ensure that a reference binds to the earlier assignment. We use MCSymbol::IsUsed and other conditions to reject potentially unsafe reassignments, but certain MCConstantExpr uses could be unsafe as well. This patch enables reassignment by cloning the symbol upon reassignment and updating the symbol table. Existing references to the original symbol remain unchanged, and the original symbol is excluded from the emitted symbol table.
2025-05-17[MC] Use *Map::try_emplace (NFC) (#140397)Kazu Hirata1-10/+7
try_emplace with is much shorter and simpler if we are default-constructing the value. While I'm at it, this patch uses structured bindings to receive the return value from try_emplace.
2025-05-10[MC] Use range-based for loops (NFC) (#139354)Kazu Hirata1-4/+4
2025-05-09MC: Support quoted symbol namesFangrui Song1-0/+21
gas has supported " quoted symbols since 2015. Both \ and " need to be escaped. https://sourceware.org/pipermail/binutils/2015-August/090003.html We don't unescape \\ or \" in assembly strings, leading to clang -c --save-temps vs clang -c difference for the following C code: ``` int x asm("a\"\\b"); ``` Fix #138390 MC/COFF/safeseh.h looks incorrect. \01 in `.safeseh "\01foo"` is not a correct escape sequence. Change it to \\ Pull Request: https://github.com/llvm/llvm-project/pull/138817
2025-05-09Revert "MC: Support quoted symbol names" (#139296)Mehdi Amini1-21/+0
Reverts llvm/llvm-project#138817 The BOLT testing is failing after this change.
2025-05-08MC: Support quoted symbol namesFangrui Song1-0/+21
gas has supported " quoted symbols since 2015: https://sourceware.org/pipermail/binutils/2015-August/090003.html We don't handle \\ or \" , leading to clang -c --save-temps vs clang -c difference for the following C code: ``` int x asm("a\"\\b"); ``` Fix #138390 MC/COFF/safeseh.h looks incorrect. \01 in `.safeseh "\01foo"` is not a correct escape sequence. Change it to \\ Pull Request: https://github.com/llvm/llvm-project/pull/138817
2025-05-06[NFC][llvm] Drop isOsWindowsOrUEFI API (#138733)Prabhu Rajasekaran1-1/+1
The Triple and SubTarget API functions isOsWindowsOrUEFI is not preferred. Dropping them.
2025-04-28[COFF] Preserve UniqueID used to create MCSectionCOFF (#123869)Haohai Wen1-1/+1
This UniqueID can be used later to create associative section. e.g. A .pseudo_probe associated to the section of the corresponding function.
2025-02-12[MC] Replace MCContext::GenericSectionID with MCSection::NonUniqueID (#126202)Haohai Wen1-3/+4
They have same semantics. NonUniqueID is more friendly for isUnique implementation in MCSectionELF. History: 97837b7 added support for unique IDs in sections and added GenericSectionID. Later, 1dc16c7 added NonUniqueID.
2025-01-28[nfc][llvm] Clean up isUEFI checks (#124845)Prabhuk1-1/+1
The check for `isOSWindows() || isUEFI()` is used in several places across the codebase. Introducing `isOSWindowsOrUEFI()` in Triple.h to simplify these checks.
2024-11-15[MC] Remove unused includes (NFC) (#116317)Kazu Hirata1-1/+0
Identified with misc-include-cleaner.
2024-10-15[Coverage][WebAssembly] Add initial support for WebAssembly/WASI (#111332)Yuta Saito1-0/+5
Currently, WebAssembly/WASI target does not provide direct support for code coverage. This patch set fixes several issues to unlock the feature. The main changes are: 1. Port `compiler-rt/lib/profile` to WebAssembly/WASI. 2. Adjust profile metadata sections for Wasm object file format. - [CodeGen] Emit `__llvm_covmap` and `__llvm_covfun` as custom sections instead of data segments. - [lld] Align the interval space of custom sections at link time. - [llvm-cov] Copy misaligned custom section data if the start address is not aligned. - [llvm-cov] Read `__llvm_prf_names` from data segments 3. [clang] Link with profile runtime libraries if requested See each commit message for more details and rationale. This is part of the effort to add code coverage support in Wasm target of Swift toolchain.
2024-07-31[MC] Remove redundant null check, NFCI (#100928)abhishek-kaushik221-1/+2
`getOrCreateSymbol` should never return a `nullptr`, add an assert and remove the redundant null check in the if condition.
2024-07-22[MC,AArch64] Create mapping symbols with non-unique namesFangrui Song1-0/+5
Add `createLocalSymbol` to create a local, non-temporary symbol. Different from `createRenamableSymbol`, the `Used` bit is ignored, therefore multiple local symbols might share the same name. Utilizing `createLocalSymbol` in AArch64 allows for efficient mapping symbol creation with non-unique names, saving .strtab space. The behavior matches GNU assembler. Pull Request: https://github.com/llvm/llvm-project/pull/99836
2024-07-22[XCOFF] refactor the XCOFF BeginSymName handlingChen Zheng1-12/+5
Fixes #96810
2024-06-29[MC] Remove addFragment. NFCFangrui Song1-1/+2
This was introduced in dcb71c06c7b059e313f22e46bc9c41343a03f1eb to help migrate away raw `operator new` and refactor the fragment representation. This is now unneeded after `MCStreamer::CurFrag` and `MCSection::CurFragList` refactoring.
2024-06-29[MC] getWasmSection: remove unused BeginSymNameFangrui Song1-5/+3
This is cargo culting for Mach-O. See #96810
2024-06-25[MC,COFF] Change how we handle section symbolsFangrui Song1-27/+36
13a79bbfe583e1d8cc85d241b580907260065eb8 (2017) unified `BeginSymbol` and section symbol for ELF. This patch does the same for COFF. * In getCOFFSection, all sections now have a `BeginSymbol` (section symbol). We do not need a dummy symbol name when `getBeginSymbol` is needed (used by AsmParser::Run and DWARF generation). * Section symbols are in the global symbol table. `call .text` will reference the section symbol instead of an undefined symbol. This matches GNU assembler. Unlike GNU, redefining the section symbol will cause a "symbol 'foo0' is already defined" error (see `section-sym-err.s`). Pull Request: https://github.com/llvm/llvm-project/pull/96459
2024-06-23[MC] Ensure all new sections have a MCDataFragmentFangrui Song1-4/+6
MCAssembler::layout ensures that every section has at least one fragment, which simplifies MCAsmLayout::getSectionAddressSize (see e73353c7201a3080851d99a16f5fe2c17f7697c6 from 2010). It's better to ensure the condition is satisfied at create time (COFF, GOFF, Mach-O) to simplify more fragment processing.
2024-06-22[MC] Change Subsection parameters from const MCExpr * to uint32_tFangrui Song1-2/+2
Follow-up to 05ba5c0648ae5e80d5afce270495bf3b1eef9af4. uint32_t is preferred over const MCExpr * in the section stack uses because it should only be evaluated once. Change the paramter type to match.
2024-06-22[MC] Allocate MCFragment with a bump allocatorFangrui Song1-2/+5
#95197 and 75006466296ed4b0f845cbbec4bf77c21de43b40 eliminated all raw `new MCXXXFragment`. We can now place fragments in a bump allocator. In addition, remove the dead `Kind == FragmentType(~0)` condition. ~CodeViewContext may call `StrTabFragment->destroy()` and need to be reset before `FragmentAllocator.Reset()`. Tested by llvm/test/MC/COFF/cv-compiler-info.ll using asan. Pull Request: https://github.com/llvm/llvm-project/pull/96402
2024-06-20[MC] Fix compilationAlexis Engelke1-1/+1
2024-06-20[CodeGen] Use temp symbol for MBBs (#95031)Alexis Engelke1-0/+11
Internal label names never occur in the symbol table, so when using an object streamer, there's no point in constructing these names and then adding them to hash tables -- they are never visible in the output. It's not possible to reuse createTempSymbol, because on BPF has a different prefix for globals and basic blocks right now.
2024-06-20[MC] Eliminate two symbol-related hash maps (#95464)aengelke1-63/+62
Previously, a symbol insertion requires (at least) three hash table operations: - Lookup/create entry in Symbols (main symbol table) - Lookup NextUniqueID to deduplicate identical temporary labels - Add entry to UsedNames, which is also used to serve as storage for the symbol name in the MCSymbol. All three lookups are done with the same name, so combining these into a single table reduces the number of lookups to one. Thus, a pointer to a symbol table entry can be passed to createSymbol to avoid a duplicate lookup of the same name. The new symbol table entry value is placed in a separate header to avoid including MCContext in MCSymbol or vice versa.
2024-06-20[MC] Remove SectionKind from MCSection (#96067)aengelke1-59/+12
There are only three actual uses of the section kind in MCSection: isText(), XCOFF, and WebAssembly. Store isText() in the MCSection, and store other info in the actual section variants where required. ELF and COFF flags also encode all relevant information, so for these two section variants, remove the SectionKind parameter entirely. This allows to remove the string switch (which is unnecessary and inaccurate) from createELFSectionImpl. This was introduced in [D133456](https://reviews.llvm.org/D133456), but apparently, it was never hit for non-writable sections anyway and the resulting kind was never used.
2024-06-14[MC] Add MCFragment allocation helpersFangrui Song1-21/+15
`allocFragment` might be changed to a placement new when the allocation strategy changes. `allocInitialFragment` is to deduplicate the following pattern ``` auto *F = new MCDataFragment(); Result->addFragment(*F); F->setParent(Result); ``` Pull Request: https://github.com/llvm/llvm-project/pull/95197
2024-06-12[MC] Remove setAllowTemporaryLabels and rename AllowTemporaryLabelsFangrui Song1-2/+2
Follow-up to a91c8398f22c28618d681497e9856c3a4b8753c3.
2024-06-12[MC] Move AllowTemporaryLabels setting to MCContext::MCContextFangrui Song1-1/+1
Also delete `AllowTemporaryLabels = true` from MCContext::reset: when llc supports -save-temp-labels in the next change, this assignment should be removed to support -compile-twice.