[llvm] [BOLT] Add reading support for Linux kernel .altinstructions section (PR #84283)

Alexander Yermolovich via llvm-commits llvm-commits at lists.llvm.org
Thu Mar 7 11:34:13 PST 2024


================
@@ -1132,6 +1156,123 @@ Error LinuxKernelRewriter::readBugTable() {
   return Error::success();
 }
 
+/// The kernel can replace certain instruction sequences depending on hardware
+/// it is running on and features specified during boot time. The information
+/// about alternative instruction sequences is stored in .altinstructions
+/// section. The format of entries in this section is defined in
+/// arch/x86/include/asm/alternative.h:
+///
+///   struct alt_instr {
+///     s32 instr_offset;
+///     s32 repl_offset;
+///     uXX feature;
+///     u8  instrlen;
+///     u8  replacementlen;
+///	    u8  padlen;         // present in older kernels
+///   } __packed;
+///
+/// Note the structures is packed.
+Error LinuxKernelRewriter::readAltInstructions() {
+  AltInstrSection = BC.getUniqueSectionByName(".altinstructions");
+  if (!AltInstrSection)
+    return Error::success();
+
+  const uint64_t Address = AltInstrSection->getAddress();
+  DataExtractor DE = DataExtractor(AltInstrSection->getContents(),
+                                   BC.AsmInfo->isLittleEndian(),
+                                   BC.AsmInfo->getCodePointerSize());
+  uint64_t EntryID = 0;
+  DataExtractor::Cursor Cursor(0);
+  while (Cursor && !DE.eof(Cursor)) {
+    const uint64_t OrgInstAddress =
+        Address + Cursor.tell() + (int32_t)DE.getU32(Cursor);
----------------
ayermolo wrote:

Doesn't coding guide prefers using c++ style casting?
https://llvm.org/docs/CodingStandards.html#prefer-c-style-casts

https://github.com/llvm/llvm-project/pull/84283


More information about the llvm-commits mailing list