LLVM integrated assembler: Improving sections and symbols
文章介绍了LLVM中的MCSection和MCSymbol类的优化改进,移除了用于区分对象文件格式的枚举类型,简化了代码结构并提高了运行效率。同时对符号内容的管理进行了调整和优化。 2025-8-17 07:0:0 Author: maskray.me(查看原文) 阅读量:8 收藏

In my previous post, LLVM integrated assembler: Improving expressions and relocations delved into enhancements made to LLVM's expression resolving and relocation generation. This post covers recent refinements to MC, focusing on sections and symbols.

Sections

1
2
3
4
5
6
7
8
9
10
11
12
class MCSection {
...
enum SectionVariant {
SV_COFF = 0,
SV_ELF,
SV_GOFF,
SV_MachO,
SV_Wasm,
SV_XCOFF,
SV_SPIRV,
SV_DXContainer,
};

Previously, the MCSection class used an enum called SectionVariant to differentiate between various object file formats, such as ELF, Mach-O, and COFF. These subclasses are used in contexts where the section type is known at compile-time, such as in MCStreamer and MCObjectTargetWriter. This change eliminates the need for runtime type information (RTTI) checks, simplifying the codebase and improving efficiency.

Additionally, the storage for fragments' fixups (adjustments to addresses and offsets) has been moved into the MCSection class.

Symbols

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class MCSymbol {
protected:
/// The kind of the symbol. If it is any value other than unset then this
/// class is actually one of the appropriate subclasses of MCSymbol.
enum SymbolKind {
SymbolKindUnset,
SymbolKindCOFF,
SymbolKindELF,
SymbolKindGOFF,
SymbolKindMachO,
SymbolKindWasm,
SymbolKindXCOFF,
};

/// A symbol can contain an Offset, or Value, or be Common, but never more
/// than one of these.
enum Contents : uint8_t {
SymContentsUnset,
SymContentsOffset,
SymContentsVariable,
SymContentsCommon,
SymContentsTargetCommon, // Index stores the section index
};

Similar to sections, the MCSymbol class also used a discriminator enum, SymbolKind, to distinguish between object file formats. This enum has also been removed.

Furthermore, the MCSymbol class had an enum Contents to specify the kind of symbol. This name was a bit confusing, so it has been renamed to enum Kind for clarity.

A special enumerator, SymContentsTargetCommon, which was used by AMDGPU for a specific type of common symbol, has also been removed. The functionality it provided is now handled by updating ELFObjectWriter to respect the symbol's section index (SHN_AMDGPU_LDS for this special AMDGPU symbol).

The previous blog post LLVM integrated assembler: Improving expressions and relocations describes other changes:

  • The MCSymbol::IsUsed flag was a workaround for detecting a subset of invalid reassignments and is removed.
  • The MCSymbol::IsResolving flag is added to detect cyclic dependencies of equated symbols.

文章来源: https://maskray.me/blog/2025-08-17-llvm-integrated-assembler-improving-sections-and-symbols
如有侵权请联系:admin#unsafe.sh