319 Commits

Author SHA1 Message Date
Fangrui Song
2d4ecba957 MCSymbolMachO: Remove classof
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-03 18:59:35 -07:00
Fangrui Song
85f00707dd MCSymbolMachO: Migrate away from classof
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-03 17:48:50 -07:00
Fangrui Song
2bebbe166b MCFragment: Migrate away from appendContents
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-28 20:35:35 -07:00
Fangrui Song
f517ac2083 MCSectionCOFF: Avoid cast
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-26 10:04:04 -07:00
Fangrui Song
ca006eeeed MCFragment: Remove clearContents and uses of non-streaming doneAppending
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-20 12:52:37 -07:00
Fangrui Song
6201761e96 MC: Rename isVirtualSection to isBssSection
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-20 10:39:17 -07:00
Fangrui Song
dc3a4c0fcf
MC: Restructure MCFragment as a fixed part and a variable tail
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-15 21:56:55 -07:00
Fangrui Song
244e053b6c MC: Remove llvm/MC/MCFixupKindInfo.h
The file used to define `MCFixupKindInfo`, a simple structure,
which is now in MCAsmBackend.h.
2025-07-05 11:24:11 -07:00
Fangrui Song
b478c38c19 MCAsmBackend: Replace FKF_IsPCRel with isPCRel() 2025-07-03 00:51:20 -07:00
Fangrui Song
9beb467d92
MC: Store fragment content and fixups out-of-line
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-07-01 00:21:12 -07:00
Fangrui Song
04395be630
MC: Merge MCFragment.h into MCSection.h
... 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-30 09:41:53 -07:00
Fangrui Song
843e362318 MachObjectWriter: Simplify Asm.getContext().reportError
Similar to b65760bc7fcdee8179bf1e57fce3786737528dd8 for ELF.
2025-05-25 13:06:14 -07:00
Fangrui Song
1193f62f7c MachObjectWriter: Remove the MCAssembler argument from getSymbolAddress 2025-05-25 12:38:08 -07:00
Fangrui Song
7ff0cf6138 MCObjectWriter: Remove the MCAssembler argument from writeObject 2025-05-24 12:55:52 -07:00
Fangrui Song
e373f7a452 MC: Simplify recordRelocation
* 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-24 09:54:03 -07:00
Fangrui Song
2849b1282e MCObjectwriter: Add getContext and simplify code 2025-05-24 09:26:30 -07:00
Fangrui Song
a8433b88fa MCObjectwriter: Add member variable MCAssembler * and simplify code 2025-05-24 00:11:32 -07:00
Fangrui Song
c239acb5b6 MCFixup: Make FixupKindInfo smaller and change getFixupKindInfo to return value
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-18 20:55:43 -07:00
Fangrui Song
976de53f10 [MC,MachO] Replace SectionAddrMap workaround with cleaner variable handling
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-06 13:07:40 -07:00
Fangrui Song
b1cd3cb3f4 [MC] Replace getSymA()->getSymbol() with getAddSym. NFC
We will replace the MCSymbolRefExpr member in MCValue with MCSymbol.
This change reduces dependence on MCSymbolRefExpr.
2025-04-05 13:34:24 -07:00
Fangrui Song
7ccdc3d5ca [MC] Replace getSymA()->getSymbol() with getAddSym. NFC
We will replace the MCSymbolRefExpr member in MCValue with MCSymbol.
This change reduces dependence on MCSymbolRefExpr.
2025-04-05 13:16:25 -07:00
Fangrui Song
b73e144bdf MCValue: Simplify code with getSubSym
MCValue::SymB is a MCSymbolRefExpr *, which might become MCSymbol * in
the future. Simplify some code that uses MCValue::SymB.
2025-03-23 12:13:13 -07:00
Fangrui Song
7722d7519c [MC] evaluateAsRelocatableImpl: remove the Fixup argument
Follow-up to d6fbffa23c84e622735b3e880fd800985c1c0072 . This commit
updates all call sites and removes the argument from the function.
2025-03-15 16:10:19 -07:00
Kazu Hirata
d73d5c8c9b
[MC] Remove unused includes (NFC) (#116317)
Identified with misc-include-cleaner.
2024-11-15 07:26:22 -08:00
Ahmed Bougacha
fd4f9520a6
[AArch64][MachO] Add ptrauth ABI version to arm64e cpusubtype. (#104650)
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-20 11:37:12 -07:00
Fangrui Song
f729c09c3a MC: Inline createMachObjectWriter into MCAsmBackend
We could do the same to COFF once WinCOFFObjectWriter is cleaned up
(#100303).
2024-07-24 11:05:05 -07:00
Fangrui Song
f017d89b22 MCAssembler: Move SubsectionsViaSymbols; to MCObjectWriter 2024-07-22 23:31:01 -07:00
Fangrui Song
eb2239299e MCAssembler: Move LinkerOptions to MachObjectWriter 2024-07-22 22:02:51 -07:00
Fangrui Song
ae3c85a708 MCAssembler: Move CGProfile to MCObjectWriter 2024-07-22 21:56:45 -07:00
Fangrui Song
09a399a1dd [MC] Move VersionInfo to MachObjectWriter 2024-07-21 13:03:21 -07:00
Fangrui Song
a2af375556 [MC] Move LOHContainer to MachObjectwriter 2024-07-21 11:19:52 -07:00
Fangrui Song
e299b163c7 [MC] Move isPrivateExtern to MCSymbolMachO 2024-07-21 10:58:20 -07:00
Eli Friedman
a10570ba91
[MachO] Detect overflow in section offset. (#98685)
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-17 09:42:53 -07:00
Fangrui Song
b75453bc07 MCAssembler: Remove unneeded non-const iterators for Sections and misleading size()
The pointers cannot be mutated even if the dereferenced MCSection can.
2024-07-05 15:42:38 -07:00
Fangrui Song
009082aa4b [MC] Move MCAssembler::DataRegions to MachObjectWriter
and make some cleanup.
2024-07-04 23:34:54 -07:00
Fangrui Song
1d4d92d1cc [MC] Move MCAssembler::IndirectSymbols to MachObjectWriter 2024-07-04 22:56:03 -07:00
Fangrui Song
a3ba6a7f97 [MC,MachO] Simplify IndirectSybols 2024-07-04 22:36:20 -07:00
Fangrui Song
94471e73fe [MC] Move MCAssembler::isSymbolLinkerVisible to MCSymbolMachO 2024-07-03 17:25:10 -07:00
Fangrui Song
1490141145
Move MCSection::LayoutOrder to MCSectionMachO
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-02 14:11:17 -07:00
Fangrui Song
4ba9956958 MachObjectWriter: replace the MCAsmLayout parameter with MCAssembler 2024-07-01 17:01:51 -07:00
Fangrui Song
e25e8003ca MCExpr::evaluateAsRelocatable: replace the MCAsmLayout parameter with MCAssembler
Continue the MCAsmLayout removal work started by 67957a45ee1ec42ae1671cdbfa0d73127346cc95.
2024-07-01 16:23:43 -07:00
Fangrui Song
c686f0cc90 MachObjectWrite::reset: clear SectionAddress and SectionOrder
Otherwise llvm/test/MC/MachO/empty-twice.ll might fail.

Fixes: 7840c0066837797cdeb62aab63044b964aa7f372 ("[MC] Move MCAsmLayout::SectionOrder to MachObjectWriter::SectionOrder")
2024-07-01 14:19:15 -07:00
Fangrui Song
7840c00668 [MC] Move MCAsmLayout::SectionOrder to MachObjectWriter::SectionOrder
Follow-up to 2c1fb411ce3aed148a278660d215e0f88ff9b9be.

SectionOrder is Mach-O specific to place zerofill sections after
non-zerofill sections in the object writer.
2024-07-01 13:17:53 -07:00
Fangrui Song
dbf12b2f77 [MC] Remove MCAsmLayout::{getSymbolOffset,getBaseSymbol}
The MCAsmLayout::* forwarders added by
67957a45ee1ec42ae1671cdbfa0d73127346cc95 have all been removed.
2024-07-01 11:51:26 -07:00
Fangrui Song
a40ca78bb9 [MC] Remove MCAsmLayout::{getSectionFileSize,getSectionAddressSize} 2024-07-01 11:27:32 -07:00
Fangrui Song
a5e905b73d [MC] Remove MCAsmLayout::getFragmentAddress 2024-07-01 11:02:15 -07:00
Fangrui Song
6b707a8cc1 [MC] Remove the MCAsmLayout parameter from MCObjectWriter::executePostLayoutBinding 2024-07-01 10:47:46 -07:00
Fangrui Song
23e6224374 [MC] Remove the MCAsmLayout parameter from MCObjectWriter::{writeObject,writeSectionData} 2024-07-01 10:04:59 -07:00
Fangrui Song
4289c422a8 [MC] Remove the MCAsmLayout parameter from MCObjectWriter::recordRelocation 2024-06-30 22:13:54 -07:00
Fangrui Song
262ad4cdf4 [MC] Remove the MCAsmLayout parameter from MCMachObjectTargetWriter 2024-06-30 21:53:35 -07:00