[clang] [llvm] [MC] Emit a jump table size section (PR #101962)

2024-09-06 Thread Nabeel Omer via cfe-commits

https://github.com/omern1 closed 
https://github.com/llvm/llvm-project/pull/101962
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [MC] Emit a jump table size section (PR #101962)

2024-08-12 Thread Nabeel Omer via cfe-commits

omern1 wrote:

> Conventionally, .debug_* sections are reserved for DWARF, even without 
> official standards.
GNU strip's default behavior --strip-all, emulated by llvm-objcopy's 
--strip-all-gnu option, is roughly to remove non-SHF_ALLOC sections that are 
not debug.
> The .llvm prefix holds no special meaning.
>
> When designing new sections, we should not adapt to the --strip-all-gnu 
> behavior.

Thanks, it appears that you're suggesting removing the `.debug_` bit from the 
name of the section, is that correct?

> Jump tables exhibit diverse structures. For instance, consider 
> llvm/test/CodeGen/ARM/jump-table-tbh.ll and Mach-O's .data_region.

Jump tables of type `EK_Inline` aren't handled in ASM printer so I think they 
should be kept out of the scope of this patch. I'll look into the Macho-O data 
region stuff.


https://github.com/llvm/llvm-project/pull/101962
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [MC] Emit a jump table size section (PR #101962)

2024-09-02 Thread Nabeel Omer via cfe-commits

https://github.com/omern1 updated 
https://github.com/llvm/llvm-project/pull/101962

>From 14f3cb82f0d7e69261bd7e1317bd66392e9a2c2b Mon Sep 17 00:00:00 2001
From: Nabeel Omer 
Date: Mon, 5 Aug 2024 11:50:18 +0100
Subject: [PATCH 01/10] [MC] Emit a jump table size section

This patch will make LLVM emit a jump table size section containing
tuples of (jump table address, entry count) in object files.
This section is useful for tools that need to statically reconstruct
the control flow of executables.

The name of the new section is .debug_llvm_jump_table_sizes
because that makes both llvm-strip and GNU strip remove it.

At the moment this is only enabled by default for the PS5 target.
---
 clang/lib/Driver/ToolChains/PS4CPU.cpp|  8 ++
 clang/test/Driver/ps4-ps5-toolchain.c |  5 +
 llvm/docs/Extensions.rst  |  6 ++
 llvm/include/llvm/BinaryFormat/ELF.h  |  1 +
 llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp| 26 +
 llvm/lib/MC/MCParser/ELFAsmParser.cpp |  2 +
 llvm/lib/MC/MCSectionELF.cpp  |  2 +
 llvm/lib/Object/ELF.cpp   |  1 +
 .../CodeGen/X86/jump-table-size-section.ll| 97 +++
 9 files changed, 148 insertions(+)
 create mode 100644 llvm/test/CodeGen/X86/jump-table-size-section.ll

diff --git a/clang/lib/Driver/ToolChains/PS4CPU.cpp 
b/clang/lib/Driver/ToolChains/PS4CPU.cpp
index a9e612c44da06a..f9a9e995fff8e2 100644
--- a/clang/lib/Driver/ToolChains/PS4CPU.cpp
+++ b/clang/lib/Driver/ToolChains/PS4CPU.cpp
@@ -265,6 +265,8 @@ void tools::PS5cpu::Linker::ConstructJob(Compilation &C, 
const JobAction &JA,
 CmdArgs.push_back(D.getLTOMode() == LTOK_Thin ? "--lto=thin"
   : "--lto=full");
 
+  AddLTOFlag("-emit-jump-table-sizes-section");
+
   if (UseJMC)
 AddLTOFlag("-enable-jmc-instrument");
 
@@ -483,6 +485,12 @@ void toolchains::PS4PS5Base::addClangTargetOptions(
 else
   CC1Args.push_back("-fvisibility-externs-nodllstorageclass=keep");
   }
+
+  // Enable jump table sizes section for PS5.
+  if (getTriple().isPS5()) {
+CC1Args.push_back("-mllvm");
+CC1Args.push_back("-emit-jump-table-sizes-section");
+  }
 }
 
 // PS4 toolchain.
diff --git a/clang/test/Driver/ps4-ps5-toolchain.c 
b/clang/test/Driver/ps4-ps5-toolchain.c
index 444e9df24714bd..c9987c2b5758b3 100644
--- a/clang/test/Driver/ps4-ps5-toolchain.c
+++ b/clang/test/Driver/ps4-ps5-toolchain.c
@@ -11,3 +11,8 @@
 // RUN: %clang %s -### -target x86_64-sie-ps5 -flto 2>&1 | FileCheck %s 
--check-prefix=LTO
 // LTO-NOT: error:
 // LTO-NOT: unable to pass LLVM bit-code
+
+// Verify that the jump table sizes section is enabled.
+// RUN: %clang %s -target x86_64-sie-ps5 -### 2>&1 | FileCheck 
-check-prefix=JUMPTABLESIZES %s
+// JUMPTABLESIZES: "-mllvm" "-emit-jump-table-sizes-section"
+// JUMPTABLESIZES: "-plugin-opt=-emit-jump-table-sizes-section"
diff --git a/llvm/docs/Extensions.rst b/llvm/docs/Extensions.rst
index 74ca8cb0aa6879..0e209f3fe5cc0d 100644
--- a/llvm/docs/Extensions.rst
+++ b/llvm/docs/Extensions.rst
@@ -554,6 +554,12 @@ time. This section is generated when the compiler enables 
fat LTO. This section
 has the ``SHF_EXCLUDE`` flag so that it is stripped from the final executable
 or shared library.
 
+``SHT_LLVM_JT_SIZES`` Section (Jump table addresses and sizes)
+^^^
+This section stores pairs of (jump table address, number of entries).
+This information is useful for tools that need to statically reconstruct
+the control flow of executables.
+
 CodeView-Dependent
 --
 
diff --git a/llvm/include/llvm/BinaryFormat/ELF.h 
b/llvm/include/llvm/BinaryFormat/ELF.h
index fb39bb4b10b377..7bec01688783d3 100644
--- a/llvm/include/llvm/BinaryFormat/ELF.h
+++ b/llvm/include/llvm/BinaryFormat/ELF.h
@@ -1121,6 +1121,7 @@ enum : unsigned {
   SHT_LLVM_BB_ADDR_MAP = 0x6fff4c0a,// LLVM Basic Block Address Map.
   SHT_LLVM_OFFLOADING = 0x6fff4c0b, // LLVM device offloading data.
   SHT_LLVM_LTO = 0x6fff4c0c,// .llvm.lto for fat LTO.
+  SHT_LLVM_JT_SIZES = 0x6fff4c0d,   // LLVM jump tables sizes.
   // Android's experimental support for SHT_RELR sections.
   // 
https://android.googlesource.com/platform/bionic/+/b7feec74547f84559a1467aca02708ff61346d2a/libc/include/elf.h#512
   SHT_ANDROID_RELR = 0x6f00,   // Relocation entries; only offsets.
diff --git a/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp 
b/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
index b64fe83959eb18..05624d2728bfdb 100644
--- a/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
+++ b/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
@@ -107,6 +107,7 @@
 #include "llvm/Pass.h"
 #include "llvm/Remarks/RemarkStreamer.h"
 #include "llvm/Support/Casting.h"
+#include "llvm/Support/CommandLine.h"
 #include "llvm/Support/Compiler.h"
 #include "llvm/Support/ErrorHandling.h"
 #include "llvm/Support/FileSystem.h"
@@ -155,6 +156,11 @@ stat

[clang] [llvm] [MC] Emit a jump table size section (PR #101962)

2024-08-05 Thread Nabeel Omer via cfe-commits

https://github.com/omern1 created 
https://github.com/llvm/llvm-project/pull/101962

This patch will make LLVM emit a jump table size section containing tuples of 
(jump table address, entry count) in object files. This section is useful for 
tools that need to statically reconstruct the control flow of executables.

The name of the new section is .debug_llvm_jump_table_sizes because that makes 
both llvm-strip and GNU strip remove it.

At the moment this is only enabled by default for the PS5 target.

>From 14f3cb82f0d7e69261bd7e1317bd66392e9a2c2b Mon Sep 17 00:00:00 2001
From: Nabeel Omer 
Date: Mon, 5 Aug 2024 11:50:18 +0100
Subject: [PATCH] [MC] Emit a jump table size section

This patch will make LLVM emit a jump table size section containing
tuples of (jump table address, entry count) in object files.
This section is useful for tools that need to statically reconstruct
the control flow of executables.

The name of the new section is .debug_llvm_jump_table_sizes
because that makes both llvm-strip and GNU strip remove it.

At the moment this is only enabled by default for the PS5 target.
---
 clang/lib/Driver/ToolChains/PS4CPU.cpp|  8 ++
 clang/test/Driver/ps4-ps5-toolchain.c |  5 +
 llvm/docs/Extensions.rst  |  6 ++
 llvm/include/llvm/BinaryFormat/ELF.h  |  1 +
 llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp| 26 +
 llvm/lib/MC/MCParser/ELFAsmParser.cpp |  2 +
 llvm/lib/MC/MCSectionELF.cpp  |  2 +
 llvm/lib/Object/ELF.cpp   |  1 +
 .../CodeGen/X86/jump-table-size-section.ll| 97 +++
 9 files changed, 148 insertions(+)
 create mode 100644 llvm/test/CodeGen/X86/jump-table-size-section.ll

diff --git a/clang/lib/Driver/ToolChains/PS4CPU.cpp 
b/clang/lib/Driver/ToolChains/PS4CPU.cpp
index a9e612c44da06..f9a9e995fff8e 100644
--- a/clang/lib/Driver/ToolChains/PS4CPU.cpp
+++ b/clang/lib/Driver/ToolChains/PS4CPU.cpp
@@ -265,6 +265,8 @@ void tools::PS5cpu::Linker::ConstructJob(Compilation &C, 
const JobAction &JA,
 CmdArgs.push_back(D.getLTOMode() == LTOK_Thin ? "--lto=thin"
   : "--lto=full");
 
+  AddLTOFlag("-emit-jump-table-sizes-section");
+
   if (UseJMC)
 AddLTOFlag("-enable-jmc-instrument");
 
@@ -483,6 +485,12 @@ void toolchains::PS4PS5Base::addClangTargetOptions(
 else
   CC1Args.push_back("-fvisibility-externs-nodllstorageclass=keep");
   }
+
+  // Enable jump table sizes section for PS5.
+  if (getTriple().isPS5()) {
+CC1Args.push_back("-mllvm");
+CC1Args.push_back("-emit-jump-table-sizes-section");
+  }
 }
 
 // PS4 toolchain.
diff --git a/clang/test/Driver/ps4-ps5-toolchain.c 
b/clang/test/Driver/ps4-ps5-toolchain.c
index 444e9df24714b..c9987c2b5758b 100644
--- a/clang/test/Driver/ps4-ps5-toolchain.c
+++ b/clang/test/Driver/ps4-ps5-toolchain.c
@@ -11,3 +11,8 @@
 // RUN: %clang %s -### -target x86_64-sie-ps5 -flto 2>&1 | FileCheck %s 
--check-prefix=LTO
 // LTO-NOT: error:
 // LTO-NOT: unable to pass LLVM bit-code
+
+// Verify that the jump table sizes section is enabled.
+// RUN: %clang %s -target x86_64-sie-ps5 -### 2>&1 | FileCheck 
-check-prefix=JUMPTABLESIZES %s
+// JUMPTABLESIZES: "-mllvm" "-emit-jump-table-sizes-section"
+// JUMPTABLESIZES: "-plugin-opt=-emit-jump-table-sizes-section"
diff --git a/llvm/docs/Extensions.rst b/llvm/docs/Extensions.rst
index 74ca8cb0aa687..0e209f3fe5cc0 100644
--- a/llvm/docs/Extensions.rst
+++ b/llvm/docs/Extensions.rst
@@ -554,6 +554,12 @@ time. This section is generated when the compiler enables 
fat LTO. This section
 has the ``SHF_EXCLUDE`` flag so that it is stripped from the final executable
 or shared library.
 
+``SHT_LLVM_JT_SIZES`` Section (Jump table addresses and sizes)
+^^^
+This section stores pairs of (jump table address, number of entries).
+This information is useful for tools that need to statically reconstruct
+the control flow of executables.
+
 CodeView-Dependent
 --
 
diff --git a/llvm/include/llvm/BinaryFormat/ELF.h 
b/llvm/include/llvm/BinaryFormat/ELF.h
index fb39bb4b10b37..7bec01688783d 100644
--- a/llvm/include/llvm/BinaryFormat/ELF.h
+++ b/llvm/include/llvm/BinaryFormat/ELF.h
@@ -1121,6 +1121,7 @@ enum : unsigned {
   SHT_LLVM_BB_ADDR_MAP = 0x6fff4c0a,// LLVM Basic Block Address Map.
   SHT_LLVM_OFFLOADING = 0x6fff4c0b, // LLVM device offloading data.
   SHT_LLVM_LTO = 0x6fff4c0c,// .llvm.lto for fat LTO.
+  SHT_LLVM_JT_SIZES = 0x6fff4c0d,   // LLVM jump tables sizes.
   // Android's experimental support for SHT_RELR sections.
   // 
https://android.googlesource.com/platform/bionic/+/b7feec74547f84559a1467aca02708ff61346d2a/libc/include/elf.h#512
   SHT_ANDROID_RELR = 0x6f00,   // Relocation entries; only offsets.
diff --git a/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp 
b/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
index b64fe83959eb1..05624d2728bf

[clang] [llvm] [MC] Emit a jump table size section (PR #101962)

2024-08-05 Thread Nabeel Omer via cfe-commits

https://github.com/omern1 updated 
https://github.com/llvm/llvm-project/pull/101962

>From 14f3cb82f0d7e69261bd7e1317bd66392e9a2c2b Mon Sep 17 00:00:00 2001
From: Nabeel Omer 
Date: Mon, 5 Aug 2024 11:50:18 +0100
Subject: [PATCH 1/2] [MC] Emit a jump table size section

This patch will make LLVM emit a jump table size section containing
tuples of (jump table address, entry count) in object files.
This section is useful for tools that need to statically reconstruct
the control flow of executables.

The name of the new section is .debug_llvm_jump_table_sizes
because that makes both llvm-strip and GNU strip remove it.

At the moment this is only enabled by default for the PS5 target.
---
 clang/lib/Driver/ToolChains/PS4CPU.cpp|  8 ++
 clang/test/Driver/ps4-ps5-toolchain.c |  5 +
 llvm/docs/Extensions.rst  |  6 ++
 llvm/include/llvm/BinaryFormat/ELF.h  |  1 +
 llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp| 26 +
 llvm/lib/MC/MCParser/ELFAsmParser.cpp |  2 +
 llvm/lib/MC/MCSectionELF.cpp  |  2 +
 llvm/lib/Object/ELF.cpp   |  1 +
 .../CodeGen/X86/jump-table-size-section.ll| 97 +++
 9 files changed, 148 insertions(+)
 create mode 100644 llvm/test/CodeGen/X86/jump-table-size-section.ll

diff --git a/clang/lib/Driver/ToolChains/PS4CPU.cpp 
b/clang/lib/Driver/ToolChains/PS4CPU.cpp
index a9e612c44da06..f9a9e995fff8e 100644
--- a/clang/lib/Driver/ToolChains/PS4CPU.cpp
+++ b/clang/lib/Driver/ToolChains/PS4CPU.cpp
@@ -265,6 +265,8 @@ void tools::PS5cpu::Linker::ConstructJob(Compilation &C, 
const JobAction &JA,
 CmdArgs.push_back(D.getLTOMode() == LTOK_Thin ? "--lto=thin"
   : "--lto=full");
 
+  AddLTOFlag("-emit-jump-table-sizes-section");
+
   if (UseJMC)
 AddLTOFlag("-enable-jmc-instrument");
 
@@ -483,6 +485,12 @@ void toolchains::PS4PS5Base::addClangTargetOptions(
 else
   CC1Args.push_back("-fvisibility-externs-nodllstorageclass=keep");
   }
+
+  // Enable jump table sizes section for PS5.
+  if (getTriple().isPS5()) {
+CC1Args.push_back("-mllvm");
+CC1Args.push_back("-emit-jump-table-sizes-section");
+  }
 }
 
 // PS4 toolchain.
diff --git a/clang/test/Driver/ps4-ps5-toolchain.c 
b/clang/test/Driver/ps4-ps5-toolchain.c
index 444e9df24714b..c9987c2b5758b 100644
--- a/clang/test/Driver/ps4-ps5-toolchain.c
+++ b/clang/test/Driver/ps4-ps5-toolchain.c
@@ -11,3 +11,8 @@
 // RUN: %clang %s -### -target x86_64-sie-ps5 -flto 2>&1 | FileCheck %s 
--check-prefix=LTO
 // LTO-NOT: error:
 // LTO-NOT: unable to pass LLVM bit-code
+
+// Verify that the jump table sizes section is enabled.
+// RUN: %clang %s -target x86_64-sie-ps5 -### 2>&1 | FileCheck 
-check-prefix=JUMPTABLESIZES %s
+// JUMPTABLESIZES: "-mllvm" "-emit-jump-table-sizes-section"
+// JUMPTABLESIZES: "-plugin-opt=-emit-jump-table-sizes-section"
diff --git a/llvm/docs/Extensions.rst b/llvm/docs/Extensions.rst
index 74ca8cb0aa687..0e209f3fe5cc0 100644
--- a/llvm/docs/Extensions.rst
+++ b/llvm/docs/Extensions.rst
@@ -554,6 +554,12 @@ time. This section is generated when the compiler enables 
fat LTO. This section
 has the ``SHF_EXCLUDE`` flag so that it is stripped from the final executable
 or shared library.
 
+``SHT_LLVM_JT_SIZES`` Section (Jump table addresses and sizes)
+^^^
+This section stores pairs of (jump table address, number of entries).
+This information is useful for tools that need to statically reconstruct
+the control flow of executables.
+
 CodeView-Dependent
 --
 
diff --git a/llvm/include/llvm/BinaryFormat/ELF.h 
b/llvm/include/llvm/BinaryFormat/ELF.h
index fb39bb4b10b37..7bec01688783d 100644
--- a/llvm/include/llvm/BinaryFormat/ELF.h
+++ b/llvm/include/llvm/BinaryFormat/ELF.h
@@ -1121,6 +1121,7 @@ enum : unsigned {
   SHT_LLVM_BB_ADDR_MAP = 0x6fff4c0a,// LLVM Basic Block Address Map.
   SHT_LLVM_OFFLOADING = 0x6fff4c0b, // LLVM device offloading data.
   SHT_LLVM_LTO = 0x6fff4c0c,// .llvm.lto for fat LTO.
+  SHT_LLVM_JT_SIZES = 0x6fff4c0d,   // LLVM jump tables sizes.
   // Android's experimental support for SHT_RELR sections.
   // 
https://android.googlesource.com/platform/bionic/+/b7feec74547f84559a1467aca02708ff61346d2a/libc/include/elf.h#512
   SHT_ANDROID_RELR = 0x6f00,   // Relocation entries; only offsets.
diff --git a/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp 
b/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
index b64fe83959eb1..05624d2728bfd 100644
--- a/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
+++ b/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
@@ -107,6 +107,7 @@
 #include "llvm/Pass.h"
 #include "llvm/Remarks/RemarkStreamer.h"
 #include "llvm/Support/Casting.h"
+#include "llvm/Support/CommandLine.h"
 #include "llvm/Support/Compiler.h"
 #include "llvm/Support/ErrorHandling.h"
 #include "llvm/Support/FileSystem.h"
@@ -155,6 +156,11 @@ static cl::bits 

[clang] [llvm] [MC] Emit a jump table size section (PR #101962)

2024-08-05 Thread Nabeel Omer via cfe-commits

https://github.com/omern1 updated 
https://github.com/llvm/llvm-project/pull/101962

>From 14f3cb82f0d7e69261bd7e1317bd66392e9a2c2b Mon Sep 17 00:00:00 2001
From: Nabeel Omer 
Date: Mon, 5 Aug 2024 11:50:18 +0100
Subject: [PATCH 1/3] [MC] Emit a jump table size section

This patch will make LLVM emit a jump table size section containing
tuples of (jump table address, entry count) in object files.
This section is useful for tools that need to statically reconstruct
the control flow of executables.

The name of the new section is .debug_llvm_jump_table_sizes
because that makes both llvm-strip and GNU strip remove it.

At the moment this is only enabled by default for the PS5 target.
---
 clang/lib/Driver/ToolChains/PS4CPU.cpp|  8 ++
 clang/test/Driver/ps4-ps5-toolchain.c |  5 +
 llvm/docs/Extensions.rst  |  6 ++
 llvm/include/llvm/BinaryFormat/ELF.h  |  1 +
 llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp| 26 +
 llvm/lib/MC/MCParser/ELFAsmParser.cpp |  2 +
 llvm/lib/MC/MCSectionELF.cpp  |  2 +
 llvm/lib/Object/ELF.cpp   |  1 +
 .../CodeGen/X86/jump-table-size-section.ll| 97 +++
 9 files changed, 148 insertions(+)
 create mode 100644 llvm/test/CodeGen/X86/jump-table-size-section.ll

diff --git a/clang/lib/Driver/ToolChains/PS4CPU.cpp 
b/clang/lib/Driver/ToolChains/PS4CPU.cpp
index a9e612c44da06..f9a9e995fff8e 100644
--- a/clang/lib/Driver/ToolChains/PS4CPU.cpp
+++ b/clang/lib/Driver/ToolChains/PS4CPU.cpp
@@ -265,6 +265,8 @@ void tools::PS5cpu::Linker::ConstructJob(Compilation &C, 
const JobAction &JA,
 CmdArgs.push_back(D.getLTOMode() == LTOK_Thin ? "--lto=thin"
   : "--lto=full");
 
+  AddLTOFlag("-emit-jump-table-sizes-section");
+
   if (UseJMC)
 AddLTOFlag("-enable-jmc-instrument");
 
@@ -483,6 +485,12 @@ void toolchains::PS4PS5Base::addClangTargetOptions(
 else
   CC1Args.push_back("-fvisibility-externs-nodllstorageclass=keep");
   }
+
+  // Enable jump table sizes section for PS5.
+  if (getTriple().isPS5()) {
+CC1Args.push_back("-mllvm");
+CC1Args.push_back("-emit-jump-table-sizes-section");
+  }
 }
 
 // PS4 toolchain.
diff --git a/clang/test/Driver/ps4-ps5-toolchain.c 
b/clang/test/Driver/ps4-ps5-toolchain.c
index 444e9df24714b..c9987c2b5758b 100644
--- a/clang/test/Driver/ps4-ps5-toolchain.c
+++ b/clang/test/Driver/ps4-ps5-toolchain.c
@@ -11,3 +11,8 @@
 // RUN: %clang %s -### -target x86_64-sie-ps5 -flto 2>&1 | FileCheck %s 
--check-prefix=LTO
 // LTO-NOT: error:
 // LTO-NOT: unable to pass LLVM bit-code
+
+// Verify that the jump table sizes section is enabled.
+// RUN: %clang %s -target x86_64-sie-ps5 -### 2>&1 | FileCheck 
-check-prefix=JUMPTABLESIZES %s
+// JUMPTABLESIZES: "-mllvm" "-emit-jump-table-sizes-section"
+// JUMPTABLESIZES: "-plugin-opt=-emit-jump-table-sizes-section"
diff --git a/llvm/docs/Extensions.rst b/llvm/docs/Extensions.rst
index 74ca8cb0aa687..0e209f3fe5cc0 100644
--- a/llvm/docs/Extensions.rst
+++ b/llvm/docs/Extensions.rst
@@ -554,6 +554,12 @@ time. This section is generated when the compiler enables 
fat LTO. This section
 has the ``SHF_EXCLUDE`` flag so that it is stripped from the final executable
 or shared library.
 
+``SHT_LLVM_JT_SIZES`` Section (Jump table addresses and sizes)
+^^^
+This section stores pairs of (jump table address, number of entries).
+This information is useful for tools that need to statically reconstruct
+the control flow of executables.
+
 CodeView-Dependent
 --
 
diff --git a/llvm/include/llvm/BinaryFormat/ELF.h 
b/llvm/include/llvm/BinaryFormat/ELF.h
index fb39bb4b10b37..7bec01688783d 100644
--- a/llvm/include/llvm/BinaryFormat/ELF.h
+++ b/llvm/include/llvm/BinaryFormat/ELF.h
@@ -1121,6 +1121,7 @@ enum : unsigned {
   SHT_LLVM_BB_ADDR_MAP = 0x6fff4c0a,// LLVM Basic Block Address Map.
   SHT_LLVM_OFFLOADING = 0x6fff4c0b, // LLVM device offloading data.
   SHT_LLVM_LTO = 0x6fff4c0c,// .llvm.lto for fat LTO.
+  SHT_LLVM_JT_SIZES = 0x6fff4c0d,   // LLVM jump tables sizes.
   // Android's experimental support for SHT_RELR sections.
   // 
https://android.googlesource.com/platform/bionic/+/b7feec74547f84559a1467aca02708ff61346d2a/libc/include/elf.h#512
   SHT_ANDROID_RELR = 0x6f00,   // Relocation entries; only offsets.
diff --git a/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp 
b/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
index b64fe83959eb1..05624d2728bfd 100644
--- a/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
+++ b/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
@@ -107,6 +107,7 @@
 #include "llvm/Pass.h"
 #include "llvm/Remarks/RemarkStreamer.h"
 #include "llvm/Support/Casting.h"
+#include "llvm/Support/CommandLine.h"
 #include "llvm/Support/Compiler.h"
 #include "llvm/Support/ErrorHandling.h"
 #include "llvm/Support/FileSystem.h"
@@ -155,6 +156,11 @@ static cl::bits 

[clang] [llvm] [MC] Emit a jump table size section (PR #101962)

2024-08-08 Thread Nabeel Omer via cfe-commits

omern1 wrote:

> The `.debug_` prefix is for DEARF content, is this being proposed to the 
> DWARF standard?

No, this isn't being proposed for the DWARF standard. Though, searching through 
the DWARF standard I couldn't find anywhere saying that the `.debug_` section 
name prefix is reserved for DWARF information. Similarly the SysV ABI and the 
ELF spec don't seem to say anything like that either. I suspect this might be a 
namespacing convention rather than a requirement.

I feel like there's a case for this prefix as GNU `strip` doesn't seem to strip 
sections with the `.llvm_` prefix and we don't people to accidentally ship 
binaries with this section in them.

Also, the choice of `.debug_llvm_` is specifically to avoid clashes with DWARF 
sections.


https://github.com/llvm/llvm-project/pull/101962
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [MC] Emit a jump table size section (PR #101962)

2024-08-08 Thread Nabeel Omer via cfe-commits

https://github.com/omern1 updated 
https://github.com/llvm/llvm-project/pull/101962

>From 14f3cb82f0d7e69261bd7e1317bd66392e9a2c2b Mon Sep 17 00:00:00 2001
From: Nabeel Omer 
Date: Mon, 5 Aug 2024 11:50:18 +0100
Subject: [PATCH 1/4] [MC] Emit a jump table size section

This patch will make LLVM emit a jump table size section containing
tuples of (jump table address, entry count) in object files.
This section is useful for tools that need to statically reconstruct
the control flow of executables.

The name of the new section is .debug_llvm_jump_table_sizes
because that makes both llvm-strip and GNU strip remove it.

At the moment this is only enabled by default for the PS5 target.
---
 clang/lib/Driver/ToolChains/PS4CPU.cpp|  8 ++
 clang/test/Driver/ps4-ps5-toolchain.c |  5 +
 llvm/docs/Extensions.rst  |  6 ++
 llvm/include/llvm/BinaryFormat/ELF.h  |  1 +
 llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp| 26 +
 llvm/lib/MC/MCParser/ELFAsmParser.cpp |  2 +
 llvm/lib/MC/MCSectionELF.cpp  |  2 +
 llvm/lib/Object/ELF.cpp   |  1 +
 .../CodeGen/X86/jump-table-size-section.ll| 97 +++
 9 files changed, 148 insertions(+)
 create mode 100644 llvm/test/CodeGen/X86/jump-table-size-section.ll

diff --git a/clang/lib/Driver/ToolChains/PS4CPU.cpp 
b/clang/lib/Driver/ToolChains/PS4CPU.cpp
index a9e612c44da06a..f9a9e995fff8e2 100644
--- a/clang/lib/Driver/ToolChains/PS4CPU.cpp
+++ b/clang/lib/Driver/ToolChains/PS4CPU.cpp
@@ -265,6 +265,8 @@ void tools::PS5cpu::Linker::ConstructJob(Compilation &C, 
const JobAction &JA,
 CmdArgs.push_back(D.getLTOMode() == LTOK_Thin ? "--lto=thin"
   : "--lto=full");
 
+  AddLTOFlag("-emit-jump-table-sizes-section");
+
   if (UseJMC)
 AddLTOFlag("-enable-jmc-instrument");
 
@@ -483,6 +485,12 @@ void toolchains::PS4PS5Base::addClangTargetOptions(
 else
   CC1Args.push_back("-fvisibility-externs-nodllstorageclass=keep");
   }
+
+  // Enable jump table sizes section for PS5.
+  if (getTriple().isPS5()) {
+CC1Args.push_back("-mllvm");
+CC1Args.push_back("-emit-jump-table-sizes-section");
+  }
 }
 
 // PS4 toolchain.
diff --git a/clang/test/Driver/ps4-ps5-toolchain.c 
b/clang/test/Driver/ps4-ps5-toolchain.c
index 444e9df24714bd..c9987c2b5758b3 100644
--- a/clang/test/Driver/ps4-ps5-toolchain.c
+++ b/clang/test/Driver/ps4-ps5-toolchain.c
@@ -11,3 +11,8 @@
 // RUN: %clang %s -### -target x86_64-sie-ps5 -flto 2>&1 | FileCheck %s 
--check-prefix=LTO
 // LTO-NOT: error:
 // LTO-NOT: unable to pass LLVM bit-code
+
+// Verify that the jump table sizes section is enabled.
+// RUN: %clang %s -target x86_64-sie-ps5 -### 2>&1 | FileCheck 
-check-prefix=JUMPTABLESIZES %s
+// JUMPTABLESIZES: "-mllvm" "-emit-jump-table-sizes-section"
+// JUMPTABLESIZES: "-plugin-opt=-emit-jump-table-sizes-section"
diff --git a/llvm/docs/Extensions.rst b/llvm/docs/Extensions.rst
index 74ca8cb0aa6879..0e209f3fe5cc0d 100644
--- a/llvm/docs/Extensions.rst
+++ b/llvm/docs/Extensions.rst
@@ -554,6 +554,12 @@ time. This section is generated when the compiler enables 
fat LTO. This section
 has the ``SHF_EXCLUDE`` flag so that it is stripped from the final executable
 or shared library.
 
+``SHT_LLVM_JT_SIZES`` Section (Jump table addresses and sizes)
+^^^
+This section stores pairs of (jump table address, number of entries).
+This information is useful for tools that need to statically reconstruct
+the control flow of executables.
+
 CodeView-Dependent
 --
 
diff --git a/llvm/include/llvm/BinaryFormat/ELF.h 
b/llvm/include/llvm/BinaryFormat/ELF.h
index fb39bb4b10b377..7bec01688783d3 100644
--- a/llvm/include/llvm/BinaryFormat/ELF.h
+++ b/llvm/include/llvm/BinaryFormat/ELF.h
@@ -1121,6 +1121,7 @@ enum : unsigned {
   SHT_LLVM_BB_ADDR_MAP = 0x6fff4c0a,// LLVM Basic Block Address Map.
   SHT_LLVM_OFFLOADING = 0x6fff4c0b, // LLVM device offloading data.
   SHT_LLVM_LTO = 0x6fff4c0c,// .llvm.lto for fat LTO.
+  SHT_LLVM_JT_SIZES = 0x6fff4c0d,   // LLVM jump tables sizes.
   // Android's experimental support for SHT_RELR sections.
   // 
https://android.googlesource.com/platform/bionic/+/b7feec74547f84559a1467aca02708ff61346d2a/libc/include/elf.h#512
   SHT_ANDROID_RELR = 0x6f00,   // Relocation entries; only offsets.
diff --git a/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp 
b/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
index b64fe83959eb18..05624d2728bfdb 100644
--- a/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
+++ b/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
@@ -107,6 +107,7 @@
 #include "llvm/Pass.h"
 #include "llvm/Remarks/RemarkStreamer.h"
 #include "llvm/Support/Casting.h"
+#include "llvm/Support/CommandLine.h"
 #include "llvm/Support/Compiler.h"
 #include "llvm/Support/ErrorHandling.h"
 #include "llvm/Support/FileSystem.h"
@@ -155,6 +156,11 @@ static

[clang] [llvm] [MC] Emit a jump table size section (PR #101962)

2024-08-08 Thread Nabeel Omer via cfe-commits

https://github.com/omern1 updated 
https://github.com/llvm/llvm-project/pull/101962

>From 14f3cb82f0d7e69261bd7e1317bd66392e9a2c2b Mon Sep 17 00:00:00 2001
From: Nabeel Omer 
Date: Mon, 5 Aug 2024 11:50:18 +0100
Subject: [PATCH 1/5] [MC] Emit a jump table size section

This patch will make LLVM emit a jump table size section containing
tuples of (jump table address, entry count) in object files.
This section is useful for tools that need to statically reconstruct
the control flow of executables.

The name of the new section is .debug_llvm_jump_table_sizes
because that makes both llvm-strip and GNU strip remove it.

At the moment this is only enabled by default for the PS5 target.
---
 clang/lib/Driver/ToolChains/PS4CPU.cpp|  8 ++
 clang/test/Driver/ps4-ps5-toolchain.c |  5 +
 llvm/docs/Extensions.rst  |  6 ++
 llvm/include/llvm/BinaryFormat/ELF.h  |  1 +
 llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp| 26 +
 llvm/lib/MC/MCParser/ELFAsmParser.cpp |  2 +
 llvm/lib/MC/MCSectionELF.cpp  |  2 +
 llvm/lib/Object/ELF.cpp   |  1 +
 .../CodeGen/X86/jump-table-size-section.ll| 97 +++
 9 files changed, 148 insertions(+)
 create mode 100644 llvm/test/CodeGen/X86/jump-table-size-section.ll

diff --git a/clang/lib/Driver/ToolChains/PS4CPU.cpp 
b/clang/lib/Driver/ToolChains/PS4CPU.cpp
index a9e612c44da06a..f9a9e995fff8e2 100644
--- a/clang/lib/Driver/ToolChains/PS4CPU.cpp
+++ b/clang/lib/Driver/ToolChains/PS4CPU.cpp
@@ -265,6 +265,8 @@ void tools::PS5cpu::Linker::ConstructJob(Compilation &C, 
const JobAction &JA,
 CmdArgs.push_back(D.getLTOMode() == LTOK_Thin ? "--lto=thin"
   : "--lto=full");
 
+  AddLTOFlag("-emit-jump-table-sizes-section");
+
   if (UseJMC)
 AddLTOFlag("-enable-jmc-instrument");
 
@@ -483,6 +485,12 @@ void toolchains::PS4PS5Base::addClangTargetOptions(
 else
   CC1Args.push_back("-fvisibility-externs-nodllstorageclass=keep");
   }
+
+  // Enable jump table sizes section for PS5.
+  if (getTriple().isPS5()) {
+CC1Args.push_back("-mllvm");
+CC1Args.push_back("-emit-jump-table-sizes-section");
+  }
 }
 
 // PS4 toolchain.
diff --git a/clang/test/Driver/ps4-ps5-toolchain.c 
b/clang/test/Driver/ps4-ps5-toolchain.c
index 444e9df24714bd..c9987c2b5758b3 100644
--- a/clang/test/Driver/ps4-ps5-toolchain.c
+++ b/clang/test/Driver/ps4-ps5-toolchain.c
@@ -11,3 +11,8 @@
 // RUN: %clang %s -### -target x86_64-sie-ps5 -flto 2>&1 | FileCheck %s 
--check-prefix=LTO
 // LTO-NOT: error:
 // LTO-NOT: unable to pass LLVM bit-code
+
+// Verify that the jump table sizes section is enabled.
+// RUN: %clang %s -target x86_64-sie-ps5 -### 2>&1 | FileCheck 
-check-prefix=JUMPTABLESIZES %s
+// JUMPTABLESIZES: "-mllvm" "-emit-jump-table-sizes-section"
+// JUMPTABLESIZES: "-plugin-opt=-emit-jump-table-sizes-section"
diff --git a/llvm/docs/Extensions.rst b/llvm/docs/Extensions.rst
index 74ca8cb0aa6879..0e209f3fe5cc0d 100644
--- a/llvm/docs/Extensions.rst
+++ b/llvm/docs/Extensions.rst
@@ -554,6 +554,12 @@ time. This section is generated when the compiler enables 
fat LTO. This section
 has the ``SHF_EXCLUDE`` flag so that it is stripped from the final executable
 or shared library.
 
+``SHT_LLVM_JT_SIZES`` Section (Jump table addresses and sizes)
+^^^
+This section stores pairs of (jump table address, number of entries).
+This information is useful for tools that need to statically reconstruct
+the control flow of executables.
+
 CodeView-Dependent
 --
 
diff --git a/llvm/include/llvm/BinaryFormat/ELF.h 
b/llvm/include/llvm/BinaryFormat/ELF.h
index fb39bb4b10b377..7bec01688783d3 100644
--- a/llvm/include/llvm/BinaryFormat/ELF.h
+++ b/llvm/include/llvm/BinaryFormat/ELF.h
@@ -1121,6 +1121,7 @@ enum : unsigned {
   SHT_LLVM_BB_ADDR_MAP = 0x6fff4c0a,// LLVM Basic Block Address Map.
   SHT_LLVM_OFFLOADING = 0x6fff4c0b, // LLVM device offloading data.
   SHT_LLVM_LTO = 0x6fff4c0c,// .llvm.lto for fat LTO.
+  SHT_LLVM_JT_SIZES = 0x6fff4c0d,   // LLVM jump tables sizes.
   // Android's experimental support for SHT_RELR sections.
   // 
https://android.googlesource.com/platform/bionic/+/b7feec74547f84559a1467aca02708ff61346d2a/libc/include/elf.h#512
   SHT_ANDROID_RELR = 0x6f00,   // Relocation entries; only offsets.
diff --git a/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp 
b/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
index b64fe83959eb18..05624d2728bfdb 100644
--- a/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
+++ b/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
@@ -107,6 +107,7 @@
 #include "llvm/Pass.h"
 #include "llvm/Remarks/RemarkStreamer.h"
 #include "llvm/Support/Casting.h"
+#include "llvm/Support/CommandLine.h"
 #include "llvm/Support/Compiler.h"
 #include "llvm/Support/ErrorHandling.h"
 #include "llvm/Support/FileSystem.h"
@@ -155,6 +156,11 @@ static

[clang] [llvm] [MC] Emit a jump table size section (PR #101962)

2024-08-20 Thread Nabeel Omer via cfe-commits

https://github.com/omern1 updated 
https://github.com/llvm/llvm-project/pull/101962

>From 14f3cb82f0d7e69261bd7e1317bd66392e9a2c2b Mon Sep 17 00:00:00 2001
From: Nabeel Omer 
Date: Mon, 5 Aug 2024 11:50:18 +0100
Subject: [PATCH 1/7] [MC] Emit a jump table size section

This patch will make LLVM emit a jump table size section containing
tuples of (jump table address, entry count) in object files.
This section is useful for tools that need to statically reconstruct
the control flow of executables.

The name of the new section is .debug_llvm_jump_table_sizes
because that makes both llvm-strip and GNU strip remove it.

At the moment this is only enabled by default for the PS5 target.
---
 clang/lib/Driver/ToolChains/PS4CPU.cpp|  8 ++
 clang/test/Driver/ps4-ps5-toolchain.c |  5 +
 llvm/docs/Extensions.rst  |  6 ++
 llvm/include/llvm/BinaryFormat/ELF.h  |  1 +
 llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp| 26 +
 llvm/lib/MC/MCParser/ELFAsmParser.cpp |  2 +
 llvm/lib/MC/MCSectionELF.cpp  |  2 +
 llvm/lib/Object/ELF.cpp   |  1 +
 .../CodeGen/X86/jump-table-size-section.ll| 97 +++
 9 files changed, 148 insertions(+)
 create mode 100644 llvm/test/CodeGen/X86/jump-table-size-section.ll

diff --git a/clang/lib/Driver/ToolChains/PS4CPU.cpp 
b/clang/lib/Driver/ToolChains/PS4CPU.cpp
index a9e612c44da06a..f9a9e995fff8e2 100644
--- a/clang/lib/Driver/ToolChains/PS4CPU.cpp
+++ b/clang/lib/Driver/ToolChains/PS4CPU.cpp
@@ -265,6 +265,8 @@ void tools::PS5cpu::Linker::ConstructJob(Compilation &C, 
const JobAction &JA,
 CmdArgs.push_back(D.getLTOMode() == LTOK_Thin ? "--lto=thin"
   : "--lto=full");
 
+  AddLTOFlag("-emit-jump-table-sizes-section");
+
   if (UseJMC)
 AddLTOFlag("-enable-jmc-instrument");
 
@@ -483,6 +485,12 @@ void toolchains::PS4PS5Base::addClangTargetOptions(
 else
   CC1Args.push_back("-fvisibility-externs-nodllstorageclass=keep");
   }
+
+  // Enable jump table sizes section for PS5.
+  if (getTriple().isPS5()) {
+CC1Args.push_back("-mllvm");
+CC1Args.push_back("-emit-jump-table-sizes-section");
+  }
 }
 
 // PS4 toolchain.
diff --git a/clang/test/Driver/ps4-ps5-toolchain.c 
b/clang/test/Driver/ps4-ps5-toolchain.c
index 444e9df24714bd..c9987c2b5758b3 100644
--- a/clang/test/Driver/ps4-ps5-toolchain.c
+++ b/clang/test/Driver/ps4-ps5-toolchain.c
@@ -11,3 +11,8 @@
 // RUN: %clang %s -### -target x86_64-sie-ps5 -flto 2>&1 | FileCheck %s 
--check-prefix=LTO
 // LTO-NOT: error:
 // LTO-NOT: unable to pass LLVM bit-code
+
+// Verify that the jump table sizes section is enabled.
+// RUN: %clang %s -target x86_64-sie-ps5 -### 2>&1 | FileCheck 
-check-prefix=JUMPTABLESIZES %s
+// JUMPTABLESIZES: "-mllvm" "-emit-jump-table-sizes-section"
+// JUMPTABLESIZES: "-plugin-opt=-emit-jump-table-sizes-section"
diff --git a/llvm/docs/Extensions.rst b/llvm/docs/Extensions.rst
index 74ca8cb0aa6879..0e209f3fe5cc0d 100644
--- a/llvm/docs/Extensions.rst
+++ b/llvm/docs/Extensions.rst
@@ -554,6 +554,12 @@ time. This section is generated when the compiler enables 
fat LTO. This section
 has the ``SHF_EXCLUDE`` flag so that it is stripped from the final executable
 or shared library.
 
+``SHT_LLVM_JT_SIZES`` Section (Jump table addresses and sizes)
+^^^
+This section stores pairs of (jump table address, number of entries).
+This information is useful for tools that need to statically reconstruct
+the control flow of executables.
+
 CodeView-Dependent
 --
 
diff --git a/llvm/include/llvm/BinaryFormat/ELF.h 
b/llvm/include/llvm/BinaryFormat/ELF.h
index fb39bb4b10b377..7bec01688783d3 100644
--- a/llvm/include/llvm/BinaryFormat/ELF.h
+++ b/llvm/include/llvm/BinaryFormat/ELF.h
@@ -1121,6 +1121,7 @@ enum : unsigned {
   SHT_LLVM_BB_ADDR_MAP = 0x6fff4c0a,// LLVM Basic Block Address Map.
   SHT_LLVM_OFFLOADING = 0x6fff4c0b, // LLVM device offloading data.
   SHT_LLVM_LTO = 0x6fff4c0c,// .llvm.lto for fat LTO.
+  SHT_LLVM_JT_SIZES = 0x6fff4c0d,   // LLVM jump tables sizes.
   // Android's experimental support for SHT_RELR sections.
   // 
https://android.googlesource.com/platform/bionic/+/b7feec74547f84559a1467aca02708ff61346d2a/libc/include/elf.h#512
   SHT_ANDROID_RELR = 0x6f00,   // Relocation entries; only offsets.
diff --git a/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp 
b/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
index b64fe83959eb18..05624d2728bfdb 100644
--- a/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
+++ b/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
@@ -107,6 +107,7 @@
 #include "llvm/Pass.h"
 #include "llvm/Remarks/RemarkStreamer.h"
 #include "llvm/Support/Casting.h"
+#include "llvm/Support/CommandLine.h"
 #include "llvm/Support/Compiler.h"
 #include "llvm/Support/ErrorHandling.h"
 #include "llvm/Support/FileSystem.h"
@@ -155,6 +156,11 @@ static

[clang] [llvm] [MC] Emit a jump table size section (PR #101962)

2024-08-20 Thread Nabeel Omer via cfe-commits

https://github.com/omern1 updated 
https://github.com/llvm/llvm-project/pull/101962

>From 14f3cb82f0d7e69261bd7e1317bd66392e9a2c2b Mon Sep 17 00:00:00 2001
From: Nabeel Omer 
Date: Mon, 5 Aug 2024 11:50:18 +0100
Subject: [PATCH 1/8] [MC] Emit a jump table size section

This patch will make LLVM emit a jump table size section containing
tuples of (jump table address, entry count) in object files.
This section is useful for tools that need to statically reconstruct
the control flow of executables.

The name of the new section is .debug_llvm_jump_table_sizes
because that makes both llvm-strip and GNU strip remove it.

At the moment this is only enabled by default for the PS5 target.
---
 clang/lib/Driver/ToolChains/PS4CPU.cpp|  8 ++
 clang/test/Driver/ps4-ps5-toolchain.c |  5 +
 llvm/docs/Extensions.rst  |  6 ++
 llvm/include/llvm/BinaryFormat/ELF.h  |  1 +
 llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp| 26 +
 llvm/lib/MC/MCParser/ELFAsmParser.cpp |  2 +
 llvm/lib/MC/MCSectionELF.cpp  |  2 +
 llvm/lib/Object/ELF.cpp   |  1 +
 .../CodeGen/X86/jump-table-size-section.ll| 97 +++
 9 files changed, 148 insertions(+)
 create mode 100644 llvm/test/CodeGen/X86/jump-table-size-section.ll

diff --git a/clang/lib/Driver/ToolChains/PS4CPU.cpp 
b/clang/lib/Driver/ToolChains/PS4CPU.cpp
index a9e612c44da06a..f9a9e995fff8e2 100644
--- a/clang/lib/Driver/ToolChains/PS4CPU.cpp
+++ b/clang/lib/Driver/ToolChains/PS4CPU.cpp
@@ -265,6 +265,8 @@ void tools::PS5cpu::Linker::ConstructJob(Compilation &C, 
const JobAction &JA,
 CmdArgs.push_back(D.getLTOMode() == LTOK_Thin ? "--lto=thin"
   : "--lto=full");
 
+  AddLTOFlag("-emit-jump-table-sizes-section");
+
   if (UseJMC)
 AddLTOFlag("-enable-jmc-instrument");
 
@@ -483,6 +485,12 @@ void toolchains::PS4PS5Base::addClangTargetOptions(
 else
   CC1Args.push_back("-fvisibility-externs-nodllstorageclass=keep");
   }
+
+  // Enable jump table sizes section for PS5.
+  if (getTriple().isPS5()) {
+CC1Args.push_back("-mllvm");
+CC1Args.push_back("-emit-jump-table-sizes-section");
+  }
 }
 
 // PS4 toolchain.
diff --git a/clang/test/Driver/ps4-ps5-toolchain.c 
b/clang/test/Driver/ps4-ps5-toolchain.c
index 444e9df24714bd..c9987c2b5758b3 100644
--- a/clang/test/Driver/ps4-ps5-toolchain.c
+++ b/clang/test/Driver/ps4-ps5-toolchain.c
@@ -11,3 +11,8 @@
 // RUN: %clang %s -### -target x86_64-sie-ps5 -flto 2>&1 | FileCheck %s 
--check-prefix=LTO
 // LTO-NOT: error:
 // LTO-NOT: unable to pass LLVM bit-code
+
+// Verify that the jump table sizes section is enabled.
+// RUN: %clang %s -target x86_64-sie-ps5 -### 2>&1 | FileCheck 
-check-prefix=JUMPTABLESIZES %s
+// JUMPTABLESIZES: "-mllvm" "-emit-jump-table-sizes-section"
+// JUMPTABLESIZES: "-plugin-opt=-emit-jump-table-sizes-section"
diff --git a/llvm/docs/Extensions.rst b/llvm/docs/Extensions.rst
index 74ca8cb0aa6879..0e209f3fe5cc0d 100644
--- a/llvm/docs/Extensions.rst
+++ b/llvm/docs/Extensions.rst
@@ -554,6 +554,12 @@ time. This section is generated when the compiler enables 
fat LTO. This section
 has the ``SHF_EXCLUDE`` flag so that it is stripped from the final executable
 or shared library.
 
+``SHT_LLVM_JT_SIZES`` Section (Jump table addresses and sizes)
+^^^
+This section stores pairs of (jump table address, number of entries).
+This information is useful for tools that need to statically reconstruct
+the control flow of executables.
+
 CodeView-Dependent
 --
 
diff --git a/llvm/include/llvm/BinaryFormat/ELF.h 
b/llvm/include/llvm/BinaryFormat/ELF.h
index fb39bb4b10b377..7bec01688783d3 100644
--- a/llvm/include/llvm/BinaryFormat/ELF.h
+++ b/llvm/include/llvm/BinaryFormat/ELF.h
@@ -1121,6 +1121,7 @@ enum : unsigned {
   SHT_LLVM_BB_ADDR_MAP = 0x6fff4c0a,// LLVM Basic Block Address Map.
   SHT_LLVM_OFFLOADING = 0x6fff4c0b, // LLVM device offloading data.
   SHT_LLVM_LTO = 0x6fff4c0c,// .llvm.lto for fat LTO.
+  SHT_LLVM_JT_SIZES = 0x6fff4c0d,   // LLVM jump tables sizes.
   // Android's experimental support for SHT_RELR sections.
   // 
https://android.googlesource.com/platform/bionic/+/b7feec74547f84559a1467aca02708ff61346d2a/libc/include/elf.h#512
   SHT_ANDROID_RELR = 0x6f00,   // Relocation entries; only offsets.
diff --git a/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp 
b/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
index b64fe83959eb18..05624d2728bfdb 100644
--- a/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
+++ b/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
@@ -107,6 +107,7 @@
 #include "llvm/Pass.h"
 #include "llvm/Remarks/RemarkStreamer.h"
 #include "llvm/Support/Casting.h"
+#include "llvm/Support/CommandLine.h"
 #include "llvm/Support/Compiler.h"
 #include "llvm/Support/ErrorHandling.h"
 #include "llvm/Support/FileSystem.h"
@@ -155,6 +156,11 @@ static

[clang] [llvm] [MC] Emit a jump table size section (PR #101962)

2024-08-22 Thread Nabeel Omer via cfe-commits

https://github.com/omern1 updated 
https://github.com/llvm/llvm-project/pull/101962

>From 14f3cb82f0d7e69261bd7e1317bd66392e9a2c2b Mon Sep 17 00:00:00 2001
From: Nabeel Omer 
Date: Mon, 5 Aug 2024 11:50:18 +0100
Subject: [PATCH 1/9] [MC] Emit a jump table size section

This patch will make LLVM emit a jump table size section containing
tuples of (jump table address, entry count) in object files.
This section is useful for tools that need to statically reconstruct
the control flow of executables.

The name of the new section is .debug_llvm_jump_table_sizes
because that makes both llvm-strip and GNU strip remove it.

At the moment this is only enabled by default for the PS5 target.
---
 clang/lib/Driver/ToolChains/PS4CPU.cpp|  8 ++
 clang/test/Driver/ps4-ps5-toolchain.c |  5 +
 llvm/docs/Extensions.rst  |  6 ++
 llvm/include/llvm/BinaryFormat/ELF.h  |  1 +
 llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp| 26 +
 llvm/lib/MC/MCParser/ELFAsmParser.cpp |  2 +
 llvm/lib/MC/MCSectionELF.cpp  |  2 +
 llvm/lib/Object/ELF.cpp   |  1 +
 .../CodeGen/X86/jump-table-size-section.ll| 97 +++
 9 files changed, 148 insertions(+)
 create mode 100644 llvm/test/CodeGen/X86/jump-table-size-section.ll

diff --git a/clang/lib/Driver/ToolChains/PS4CPU.cpp 
b/clang/lib/Driver/ToolChains/PS4CPU.cpp
index a9e612c44da06a..f9a9e995fff8e2 100644
--- a/clang/lib/Driver/ToolChains/PS4CPU.cpp
+++ b/clang/lib/Driver/ToolChains/PS4CPU.cpp
@@ -265,6 +265,8 @@ void tools::PS5cpu::Linker::ConstructJob(Compilation &C, 
const JobAction &JA,
 CmdArgs.push_back(D.getLTOMode() == LTOK_Thin ? "--lto=thin"
   : "--lto=full");
 
+  AddLTOFlag("-emit-jump-table-sizes-section");
+
   if (UseJMC)
 AddLTOFlag("-enable-jmc-instrument");
 
@@ -483,6 +485,12 @@ void toolchains::PS4PS5Base::addClangTargetOptions(
 else
   CC1Args.push_back("-fvisibility-externs-nodllstorageclass=keep");
   }
+
+  // Enable jump table sizes section for PS5.
+  if (getTriple().isPS5()) {
+CC1Args.push_back("-mllvm");
+CC1Args.push_back("-emit-jump-table-sizes-section");
+  }
 }
 
 // PS4 toolchain.
diff --git a/clang/test/Driver/ps4-ps5-toolchain.c 
b/clang/test/Driver/ps4-ps5-toolchain.c
index 444e9df24714bd..c9987c2b5758b3 100644
--- a/clang/test/Driver/ps4-ps5-toolchain.c
+++ b/clang/test/Driver/ps4-ps5-toolchain.c
@@ -11,3 +11,8 @@
 // RUN: %clang %s -### -target x86_64-sie-ps5 -flto 2>&1 | FileCheck %s 
--check-prefix=LTO
 // LTO-NOT: error:
 // LTO-NOT: unable to pass LLVM bit-code
+
+// Verify that the jump table sizes section is enabled.
+// RUN: %clang %s -target x86_64-sie-ps5 -### 2>&1 | FileCheck 
-check-prefix=JUMPTABLESIZES %s
+// JUMPTABLESIZES: "-mllvm" "-emit-jump-table-sizes-section"
+// JUMPTABLESIZES: "-plugin-opt=-emit-jump-table-sizes-section"
diff --git a/llvm/docs/Extensions.rst b/llvm/docs/Extensions.rst
index 74ca8cb0aa6879..0e209f3fe5cc0d 100644
--- a/llvm/docs/Extensions.rst
+++ b/llvm/docs/Extensions.rst
@@ -554,6 +554,12 @@ time. This section is generated when the compiler enables 
fat LTO. This section
 has the ``SHF_EXCLUDE`` flag so that it is stripped from the final executable
 or shared library.
 
+``SHT_LLVM_JT_SIZES`` Section (Jump table addresses and sizes)
+^^^
+This section stores pairs of (jump table address, number of entries).
+This information is useful for tools that need to statically reconstruct
+the control flow of executables.
+
 CodeView-Dependent
 --
 
diff --git a/llvm/include/llvm/BinaryFormat/ELF.h 
b/llvm/include/llvm/BinaryFormat/ELF.h
index fb39bb4b10b377..7bec01688783d3 100644
--- a/llvm/include/llvm/BinaryFormat/ELF.h
+++ b/llvm/include/llvm/BinaryFormat/ELF.h
@@ -1121,6 +1121,7 @@ enum : unsigned {
   SHT_LLVM_BB_ADDR_MAP = 0x6fff4c0a,// LLVM Basic Block Address Map.
   SHT_LLVM_OFFLOADING = 0x6fff4c0b, // LLVM device offloading data.
   SHT_LLVM_LTO = 0x6fff4c0c,// .llvm.lto for fat LTO.
+  SHT_LLVM_JT_SIZES = 0x6fff4c0d,   // LLVM jump tables sizes.
   // Android's experimental support for SHT_RELR sections.
   // 
https://android.googlesource.com/platform/bionic/+/b7feec74547f84559a1467aca02708ff61346d2a/libc/include/elf.h#512
   SHT_ANDROID_RELR = 0x6f00,   // Relocation entries; only offsets.
diff --git a/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp 
b/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
index b64fe83959eb18..05624d2728bfdb 100644
--- a/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
+++ b/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
@@ -107,6 +107,7 @@
 #include "llvm/Pass.h"
 #include "llvm/Remarks/RemarkStreamer.h"
 #include "llvm/Support/Casting.h"
+#include "llvm/Support/CommandLine.h"
 #include "llvm/Support/Compiler.h"
 #include "llvm/Support/ErrorHandling.h"
 #include "llvm/Support/FileSystem.h"
@@ -155,6 +156,11 @@ static

[clang] [llvm] [MC] Emit a jump table size section (PR #101962)

2024-08-22 Thread Nabeel Omer via cfe-commits


@@ -2764,6 +2770,27 @@ void AsmPrinter::emitJumpTableInfo() {
 for (const MachineBasicBlock *MBB : JTBBs)
   emitJumpTableEntry(MJTI, MBB, JTI);
   }
+
+  if (EmitJumpTableSizesSection && TM.getTargetTriple().isOSBinFormatELF() &&

omern1 wrote:

I've added support for COFF (hopefully correctly). I'm completely unfamiliar 
with MachO so I suggest that we leave that for a followup.

https://github.com/llvm/llvm-project/pull/101962
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [MC] Emit a jump table size section (PR #101962)

2025-02-14 Thread Nabeel Omer via cfe-commits

omern1 wrote:

Hi @ruotongyu,

> and am trying to apply it to more architectures, such as x86.

I'm not sure what you mean here, this patch is really architecture independent 
and already works on X86. I maybe be misunderstanding you here though.

> Do you have any suggestions on how to correctly associate the jump table with 
> the corresponding assembly instruction? Please let me know if I’m 
> misunderstanding anything.

That's unfortunately something that I did not have time to do in this patch but 
https://github.com/llvm/llvm-project/pull/112606 is trying to achieve that.

https://github.com/llvm/llvm-project/pull/101962
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits