294 Commits

Author SHA1 Message Date
Fangrui Song
eea7d32bd2 [MC] Move fixSymbolsInTLSFixups to ELFObjectWriter
so that we only need to do it once during recordRelocation. In the
future, we should change fixSymbolsInTLSFixups to apply to MCValue
instead of MCExpr, similar to GNU assembler.
2025-03-12 19:49:52 -07:00
Fangrui Song
fe56c4c019 [MC] Remove unneeded VK_None argument from MCSymbolRefExpr::create. NFC 2025-03-05 23:14:04 -08:00
Fangrui Song
0301580580 [PowerPC] Remove VK_PPC_TLSGD and VK_PPC_TLSLD
52cf8e44880bcf614068b66b63393aa8da1edd76 (2013) introduced the
VK_PPC_TLSGD workaround to prevent unconditional reference to
_GLOBAL_OFFSET_TABLE_ in ELFObjectWriter.

e2b355d651ed8f2cbe61672c4c39b6419e471265 (2015) removed the
`_GLOBAL_OFFSET_TABLE_` hack for the generic VK_TLSGD,
making the VK_PPC_TLSGD workaround unneeded.
2025-03-02 22:25:59 -08:00
Fangrui Song
ad61e53333
[ARM] Move MCStreamer::emitThumbFunc to ARMTargetStreamer
MCStreamer should not declare arch-specific functions. Such functions
should go to MCTargetStreamer.

Move MCMachOStreamer::emitThumbFunc to ARMTargetMachOStreamer, which is
a new subclass of ARMTargetStreamer. (The new class is just placed in
ARMMachObjectWriter.cpp. The conventional split like
ARMELFObjectWriter.cpp/ARMELFObjectWriter.cpp is overkill.)

`emitCFILabel`, called by ARMWinCOFFStreamer.cpp, has to be made public.

Pull Request: https://github.com/llvm/llvm-project/pull/126199
2025-02-10 09:40:43 -08:00
SivanShani-Arm
ee99c4d484
[LLVM][Clang][AArch64] Implement AArch64 build attributes (#123990)
- Added support for AArch64-specific build attributes.
- Print AArch64 build attributes to assembly.
- Emit AArch64 build attributes to ELF.

Specification: https://github.com/ARM-software/abi-aa/pull/230
2025-01-23 09:46:59 +00:00
Kazu Hirata
b40739a6e9 Revert "[LLVM][Clang][AArch64] Implement AArch64 build attributes (#118771)"
This reverts commit d7fb4a275c98f4035d1083b5eb3edd2ffb2da00e.

Buildbots failing:
https://lab.llvm.org/buildbot/#/builders/169/builds/7671
https://lab.llvm.org/buildbot/#/builders/65/builds/11046
2025-01-22 10:12:27 -08:00
SivanShani-Arm
d7fb4a275c
[LLVM][Clang][AArch64] Implement AArch64 build attributes (#118771)
- Added support for AArch64-specific build attributes.
- Print AArch64 build attributes to assembly.
- Parse AArch64 build attributes from assembly.
- Emit AArch64 build attributes to ELF.

Specification:
 https://github.com/ARM-software/abi-aa/pull/230
2025-01-22 14:23:46 +00:00
Kazu Hirata
d73d5c8c9b
[MC] Remove unused includes (NFC) (#116317)
Identified with misc-include-cleaner.
2024-11-15 07:26:22 -08:00
Sergei Barannikov
25bea3eb03
[MC] Forward declare ELFObjectWriter (#100989) 2024-07-30 10:40:40 +03:00
Fangrui Song
f017d89b22 MCAssembler: Move SubsectionsViaSymbols; to MCObjectWriter 2024-07-22 23:31:01 -07:00
Fangrui Song
ae3c85a708 MCAssembler: Move CGProfile to MCObjectWriter 2024-07-22 21:56:45 -07:00
Fangrui Song
9e97f80cc5 MCAssembler: Move Symvers to ELFObjectWriter
Similar to c473e75adeaf2998e4fb444b0bdbf2dd19312e50
2024-07-22 19:33:00 -07:00
Fangrui Song
70c52b62c5 [MC] Export llvm::ELFObjectWriter
Similar to commit 28fcafb50274be2520117eacb0a886adafefe59d (2011) for
MachObjectWriter and commit 9539a7796094ff5fb59d9c685140ea2e214b945c for
WinCOFFObjectWriter.

MCELFStreamer can now access ELFObjectWriter directly without adding
ELF-specific markGnuAbi (https://reviews.llvm.org/D97976) and
setOverrideABIVersion to MCObjectWriter.

A few member variables have to be made public since we cannot use a
friend declaration for ELFWriter.
2024-07-22 16:18:25 -07:00
Kazu Hirata
b96c0123fd
[MC] Use range-based for loops (NFC) (#98604) 2024-07-12 11:37:27 -07:00
Alexis Engelke
f15266e9d5
[MC][ELF] Emit instructions directly into fragment (#94950)
Avoid needless copying of instructions and fixups and directly emit into
the fragment small vectors.

This (optionally, second commit) also removes the single use of the
MCCompactEncodedInstFragment to simplify code.
2024-07-04 16:46:31 +02:00
Fangrui Song
626eef5ecf [MC] Optimize getCurrentSectionOnly using CurFrag and make it non-nullable
Follow-up to e48c4011ca80385573f1b92793c75dc98abb228f ("[MC] Cache current fragment in MCStreamer").

Prerequisite: a few commits that removed nullable getCurrentSectionOnly calls.
2024-06-27 22:51:30 -07:00
Fangrui Song
db48f1a176 [MC] Remove nullable getCurrentSectionOnly use from AsmParser
We will implement getCurrentSectionOnly with `CurFrag->getParent()`,
which is non-null. Eliminate a nullable use.
2024-06-27 22:37:37 -07:00
Fangrui Song
fcffb2c024 [MC] Replace one nullable getCurrentSectionOnly with CurFrag
We will implement getCurrentSectionOnly with `CurFrag->getParent()`,
which is non-null. Eliminate a nullable use.
2024-06-27 22:27:14 -07:00
Fangrui Song
95f983f823 [MC] Change Subsection parameters from const MCExpr * to uint32_t
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 21:48:11 -07:00
Fangrui Song
4684d0c007 [MC] emitLabelAtPos: change parameter to MCDataFragment &. NFC
emitLabelAtPos is only called by ARMELFStreamer with MCDataFragment.
2024-06-21 23:46:58 -07:00
Fangrui Song
b1932b8483
[MC] Aligned bundling: remove special handling for RelaxAll
When both aligned bundling and RelaxAll are enabled, bundle padding is
directly written into fragments (https://reviews.llvm.org/D8072).
(The original motivation was memory usage, which has been achieved from
different angles with recent assembler improvement).

The code presents challenges with the work to replace fragment
representation (e.g. #94950 #95077). This patch removes the special
handling. RelaxAll still works but the behavior seems slightly different
as revealed by 2 changed tests. However, most `-mc-relax-all` tests are
unchanged.

RelaxAll used to be the default for clang -O0. This mode has significant
code size drawbacks and newer Clang doesn't use it (#90013).

---

flushPendingLabels: The FOffset parameter can be removed: pending labels
will be assigned to the incoming fragment at offset 0.

Pull Request: https://github.com/llvm/llvm-project/pull/95188
2024-06-14 10:01:36 -07:00
Fangrui Song
f808abf508
[MC] Add MCFragment allocation helpers
`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-14 09:39:32 -07:00
Fangrui Song
4e34035616 [MC] Remove RelaxAll parameters from create*Streamer
Related to clean-up opportunities discussed at #90013.

After these cleanups, the `RelaxAll` parameter from
`createMCObjectStreamer` can be removed as well. As
`createMCObjectStreamer` is a more user-facing API and used by two files
in mlir/, we postpone the cleanup to the future.
2024-04-25 14:57:27 -07:00
Fangrui Song
45b59cb1d4 [MC] Move setRelaxAll() calls to MCObjectStreamer
Related to clean-up opportunities discussed at #90013.
2024-04-25 13:54:04 -07:00
Fangrui Song
8275dc9748 [MC] .reloc: register used symbols
When `sym` in `.reloc ., BFD_RELOC_NONE, sym` is not referenced
elsewhere, `sym` is not in the symbol table and the relocation
references the null symbol. Visit the expression to fix the issue.
2023-12-07 18:34:36 -08:00
Fangrui Song
e6fed06335 [RISCV] Make linker-relaxable instructions terminate MCDataFragment
`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-29 09:39:57 -07:00
Eli Friedman
7198baccda [COFF] Add MC support for emitting IMAGE_WEAK_EXTERN_ANTI_DEPENDENCY symbols
This is mostly useful for ARM64EC, which uses such symbols extensively.

One interesting quirk of ARM64EC is that we need to be able to emit weak
symbols that point at each other (so if either symbol is defined
elsewhere, both symbols point at the definition). This handling is
currently restricted to weak_anti_dep symbols, because we depend on the
current behavior of resolving weak symbols in some cases.

Differential Revision: https://reviews.llvm.org/D145208
2023-06-07 11:07:21 -07:00
Zequan Wu
439f804c47 Revert "[COFF] Add MC support for emitting IMAGE_WEAK_EXTERN_ANTI_DEPENDENCY symbols"
This reverts commit 10c17c97ebaf81ac26f6830e51a7a57ddcf63cd2. It causes undefined symbol error on chromium windows build. A small repro was uploaded to the code review.
2023-04-27 10:01:56 -04:00
Eli Friedman
10c17c97eb [COFF] Add MC support for emitting IMAGE_WEAK_EXTERN_ANTI_DEPENDENCY symbols
This is mostly useful for ARM64EC, which uses such symbols extensively.

One interesting quirk of ARM64EC is that we need to be able to emit weak
symbols that point at each other (so if either symbol is defined
elsewhere, both symbols point at the definition).  This required a few
changes to the way we handle weak symbols on Windows.

Differential Revision: https://reviews.llvm.org/D145208
2023-04-17 13:17:25 -07:00
Arthur Eubanks
29a88f991b Revert "[COFF] Add MC support for emitting IMAGE_WEAK_EXTERN_ANTI_DEPENDENCY symbols"
This reverts commit fffdb7eac58b4efde5e23c1281e7a7f93a42d280.

Causes crashes, see https://reviews.llvm.org/D145208
2023-04-13 09:09:36 -07:00
Eli Friedman
fffdb7eac5 [COFF] Add MC support for emitting IMAGE_WEAK_EXTERN_ANTI_DEPENDENCY symbols
This is mostly useful for ARM64EC, which uses such symbols extensively.

One interesting quirk of ARM64EC is that we need to be able to emit weak
symbols that point at each other (so if either symbol is defined
elsewhere, both symbols point at the definition).  This required a few
changes to the way we handle weak symbols on Windows.

Differential Revision: https://reviews.llvm.org/D145208
2023-04-07 14:05:45 -07:00
Alexis Engelke
0c049ea60a [MC] Always encode instruction into SmallVector
All users of MCCodeEmitter::encodeInstruction use a raw_svector_ostream
to encode the instruction into a SmallVector. The raw_ostream however
incurs some overhead for the actual encoding.

This change allows an MCCodeEmitter to directly emit an instruction into
a SmallVector without using a raw_ostream and therefore allow for
performance improvments in encoding. A default path that uses existing
raw_ostream implementations is provided.

Reviewed By: MaskRay, Amir

Differential Revision: https://reviews.llvm.org/D145791
2023-04-06 16:21:49 +02:00
Guillaume Chatelet
070bc71830 [Alignment][NFC] Use Align in MCSymbol::declareCommon 2022-12-12 14:29:04 +00:00
Guillaume Chatelet
5bf8e14350 [Alignment] Use Align in MCStreamer::emitCommonSymbol
Next patch after D139548 and D139439. Same expectations, the change seems safe with as far as llvm goes, we cannot check downstream implementations.

Differential Revision: https://reviews.llvm.org/D139614
2022-12-09 10:10:34 +00:00
Guillaume Chatelet
b94bd9c20a [Alignment] Use Align in MCStreamer::emitTBSSSymbol
In the same vein as D139439, the patch is not NFC as there is no way to check all downstream implementations but the patch seems pretty safe.

Differential Revision: https://reviews.llvm.org/D139548
2022-12-08 09:02:28 +00:00
Guillaume Chatelet
7203a8614a [reland][Alignment] Use Align in MCStreamer emitZeroFill/emitLocalCommonSymbol
Before performing this change, I checked that `ByteAlignment` was never `0` inside `MCAsmStreamer:emitZeroFill` and `MCAsmStreamer::emitLocalCommonSymbol`.
I believe it is NFC as `0` values are illegal in `emitZeroFill` anyways, `Log2(ByteAlignment)` would be undefined.
And currently, all calls to `emitLocalCommonSymbol` are provably `>0`.

Differential Revision: https://reviews.llvm.org/D139439
2022-12-07 14:54:03 +00:00
Guillaume Chatelet
b822063669 Revert D139439 "[Alignment] Use Align in MCStreamer emitZeroFill/emitLocalCommonSymbol"
This breaks Windows bots with
`warning C4334: '<<': result of 32-bit shift implicitly converted to 64 bits (was 64-bit shift intended?)`

Some shift operators are lacking a proper literal unit ('1ULL' instead of
'1'). Will reland once fixed.

This reverts commit c621c1a8e81856e6bf2be79714767d80466e9ede.
2022-12-07 14:51:26 +00:00
Guillaume Chatelet
c621c1a8e8 [Alignment] Use Align in MCStreamer emitZeroFill/emitLocalCommonSymbol
Before performing this change, I checked that `ByteAlignment` was never `0` inside `MCAsmStreamer:emitZeroFill` and `MCAsmStreamer::emitLocalCommonSymbol`.
I believe it is NFC as `0` values are illegal in `emitZeroFill` anyways, `Log2(ByteAlignment)` would be undefined.
And currently, all calls to `emitLocalCommonSymbol` are provably `>0`.

Differential Revision: https://reviews.llvm.org/D139439
2022-12-07 14:29:16 +00:00
Fangrui Song
f4c16c4473 [MC] llvm::Optional => std::optional
https://discourse.llvm.org/t/deprecating-llvm-optional-x-hasvalue-getvalue-getvalueor/63716
2022-12-04 21:36:08 +00:00
Mitch Phillips
850defb861 Add assembler plumbing for sanitize_memtag
Extends the Asm reader/writer to support reading and writing the
'.memtag' directive (including allowing it on internal global
variables). Also add some extra tooling support, including objdump and
yaml2obj/obj2yaml.

Test that the sanitize_memtag IR attribute produces the expected asm
directive.

Uses the new Aarch64 MemtagABI specification
(https://github.com/ARM-software/abi-aa/blob/main/memtagabielf64/memtagabielf64.rst)
to identify symbols as tagged in object files. This is done using a
R_AARCH64_NONE relocation that identifies each tagged symbol, and these
relocations are tagged in a special SHT_AARCH64_MEMTAG_GLOBALS_STATIC
section. This signals to the linker that the global variable should be
tagged.

Reviewed By: fmayer, MaskRay, peter.smith

Differential Revision: https://reviews.llvm.org/D128958
2022-12-01 10:50:34 -08:00
Guillaume Chatelet
702126aec5 [NFC] Add helper method to ensure min alignment on MCSection
Follow up on D138653.

Differential Revision: https://reviews.llvm.org/D138686
2022-11-28 10:00:34 +00:00
Guillaume Chatelet
6c09ea3fdd [Alignment][NFC] Use Align in MCStreamer::emitValueToAlignment
Differential Revision: https://reviews.llvm.org/D138674
2022-11-24 16:09:44 +00:00
Guillaume Chatelet
4f17734175 [Alignment][NFC] Use Align in MCStreamer::emitCodeAlignment
This patch makes code less readable but it will clean itself after all functions are converted.

Differential Revision: https://reviews.llvm.org/D138665
2022-11-24 14:51:46 +00:00
Guillaume Chatelet
99089b490d [Alignment][NFC] Use Align in MCStreamer::emitBundleAlignMode
Summary:

Reviewers: courbet

Subscribers:
2022-11-24 14:35:01 +00:00
Guillaume Chatelet
e647b4f519 [reland][Alignment][NFC] Use the Align type in MCSection
Differential Revision: https://reviews.llvm.org/D138653
2022-11-24 13:19:18 +00:00
Guillaume Chatelet
3467f9c7d6 Revert D138653 [Alignment][NFC] Use the Align type in MCSection"
This breaks the bolt project.
This reverts commit 409f0dc4a420db1c6b259d5ae965a070c169d930.
2022-11-24 12:42:30 +00:00
Guillaume Chatelet
409f0dc4a4 [Alignment][NFC] Use the Align type in MCSection
Differential Revision: https://reviews.llvm.org/D138653
2022-11-24 12:32:58 +00:00
Adhemerval Zanella
abcb3d58d9 [MC][ELF] Mark GNU ABI if ifunc are used
Similar to D107861.  Some tools required the GNU ABI mark to output
the symbol is a IFUNC type correctly (for instance binutils readelf).

Reviewed By: MaskRay

Differential Revision: https://reviews.llvm.org/D131162
2022-08-25 18:19:10 -03:00
Fangrui Song
adf4142f76 [MC] De-capitalize SwitchSection. NFC
Add SwitchSection to return switchSection. The API will be removed soon.
2022-06-10 22:50:55 -07:00
Fangrui Song
15d82c62dc [MC] De-capitalize MCStreamer functions
Follow-up to c031378ce01b8485ba0ef486654bc9393c4ac024 .
The class is mostly consistent now.
2022-06-07 00:31:02 -07:00