Skip to content

[COFF] Preserve UniqueID used to create MCSectionCOFF #123869

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

HaohaiWen
Copy link
Contributor

This UniqueID can be used later to create associative section.
e.g. A .pseudo_probe associated to the section of the corresponding
function.

This UniqueID can be used later to create associative section.
e.g. A .pseudo_probe associated to the section of the corresponding
function.
@llvmbot llvmbot added the mc Machine (object) code label Jan 22, 2025
@llvmbot
Copy link
Member

llvmbot commented Jan 22, 2025

@llvm/pr-subscribers-backend-x86

@llvm/pr-subscribers-mc

Author: Haohai Wen (HaohaiWen)

Changes

This UniqueID can be used later to create associative section.
e.g. A .pseudo_probe associated to the section of the corresponding
function.


Full diff: https://github.com/llvm/llvm-project/pull/123869.diff

2 Files Affected:

  • (modified) llvm/include/llvm/MC/MCSectionCOFF.h (+7-2)
  • (modified) llvm/lib/MC/MCContext.cpp (+1-1)
diff --git a/llvm/include/llvm/MC/MCSectionCOFF.h b/llvm/include/llvm/MC/MCSectionCOFF.h
index 97540d985dbd1d..9c79c27f2a9152 100644
--- a/llvm/include/llvm/MC/MCSectionCOFF.h
+++ b/llvm/include/llvm/MC/MCSectionCOFF.h
@@ -47,16 +47,19 @@ class MCSectionCOFF final : public MCSection {
   /// section (Characteristics & IMAGE_SCN_LNK_COMDAT) != 0
   mutable int Selection;
 
+  unsigned UniqueID;
+
 private:
   friend class MCContext;
   // The storage of Name is owned by MCContext's COFFUniquingMap.
   MCSectionCOFF(StringRef Name, unsigned Characteristics,
-                MCSymbol *COMDATSymbol, int Selection, MCSymbol *Begin)
+                MCSymbol *COMDATSymbol, int Selection, unsigned UniqueID,
+                MCSymbol *Begin)
       : MCSection(SV_COFF, Name, Characteristics & COFF::IMAGE_SCN_CNT_CODE,
                   Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA,
                   Begin),
         Characteristics(Characteristics), COMDATSymbol(COMDATSymbol),
-        Selection(Selection) {
+        Selection(Selection), UniqueID(UniqueID) {
     assert((Characteristics & 0x00F00000) == 0 &&
            "alignment must not be set upon section creation");
   }
@@ -72,6 +75,8 @@ class MCSectionCOFF final : public MCSection {
 
   void setSelection(int Selection) const;
 
+  unsigned getUniqueID() const { return UniqueID; }
+
   void printSwitchToSection(const MCAsmInfo &MAI, const Triple &T,
                             raw_ostream &OS,
                             uint32_t Subsection) const override;
diff --git a/llvm/lib/MC/MCContext.cpp b/llvm/lib/MC/MCContext.cpp
index 46222fcaa5b152..56aba5de4445e4 100644
--- a/llvm/lib/MC/MCContext.cpp
+++ b/llvm/lib/MC/MCContext.cpp
@@ -718,7 +718,7 @@ MCSectionCOFF *MCContext::getCOFFSection(StringRef Section,
   StringRef CachedName = Iter->first.SectionName;
   MCSymbol *Begin = getOrCreateSectionSymbol<MCSymbolCOFF>(Section);
   MCSectionCOFF *Result = new (COFFAllocator.Allocate()) MCSectionCOFF(
-      CachedName, Characteristics, COMDATSymbol, Selection, Begin);
+      CachedName, Characteristics, COMDATSymbol, Selection, UniqueID, Begin);
   Iter->second = Result;
   auto *F = allocInitialFragment(*Result);
   Begin->setFragment(F);

@MaskRay
Copy link
Member

MaskRay commented Jan 22, 2025

Best to add .section parser and printer support so that this feature is testable. The new test can be added to llvm/test/MC/COFF/section-unique.s

@mstorsjo might be interested in ensuring gas compatibility.

@HaohaiWen
Copy link
Contributor Author

HaohaiWen commented Feb 8, 2025

Best to add .section parser and printer support so that this feature is testable. The new test can be added to llvm/test/MC/COFF/section-unique.s

@mstorsjo might be interested in ensuring gas compatibility.

Is there any specification about COFF .section directive?
Current format is
.section section_name,"characteristic"[(,selection,comdat_name) | (\n.linkonce selection)]
If we can define .section directive format, I'd like to define it as
.section section_name,"characteristic"[(,selection,comdat_name)][,unique,unique_id]
.section section_name,"characteristic"[,unique,unique_id][(\n.linkonce selection)]
Any suggestion about that?

@HaohaiWen
Copy link
Contributor Author

Let's revive this PR. Any comments?

Copy link
Contributor

@williamweixiao williamweixiao left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backend:X86 mc Machine (object) code
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants