[llvm] [Object] Extract format-agnostic BBAddrMap decoder (PR #188435)

Rahman Lavaee via llvm-commits llvm-commits at lists.llvm.org
Mon Mar 30 10:56:24 PDT 2026


================
@@ -0,0 +1,230 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// This file implements shared utilities for basic-block address maps.
+///
+//===----------------------------------------------------------------------===//
+
+#include "llvm/Object/BBAddrMap.h"
+#include "llvm/Object/Error.h"
+#include "llvm/Support/DataExtractor.h"
+
+using namespace llvm;
+using namespace object;
+
+namespace {
+
+// Helper to extract and decode the next ULEB128 value as unsigned int.
+// Returns zero and sets ULEBSizeErr if the ULEB128 value exceeds the unsigned
+// int limit.
+// Also returns zero if ULEBSizeErr is already in an error state.
+// ULEBSizeErr is an out variable if an error occurs.
+template <typename IntTy, std::enable_if_t<std::is_unsigned_v<IntTy>, int> = 0>
+static IntTy readULEB128As(DataExtractor &Data, DataExtractor::Cursor &Cur,
+                           Error &ULEBSizeErr) {
+  // Bail out and do not extract data if ULEBSizeErr is already set.
+  if (ULEBSizeErr)
+    return 0;
+  uint64_t Offset = Cur.tell();
+  uint64_t Value = Data.getULEB128(Cur);
+  if (Value > std::numeric_limits<IntTy>::max()) {
+    ULEBSizeErr = createError("ULEB128 value at offset 0x" +
+                              Twine::utohexstr(Offset) + " exceeds UINT" +
+                              Twine(std::numeric_limits<IntTy>::digits) +
+                              "_MAX (0x" + Twine::utohexstr(Value) + ")");
+    return 0;
+  }
+  return static_cast<IntTy>(Value);
+}
+} // end anonymous namespace
+
+Expected<std::vector<BBAddrMap>> llvm::object::decodeBBAddrMapPayload(
+    ArrayRef<uint8_t> Content, bool IsLittleEndian, uint8_t AddressSize,
----------------
rlavaee wrote:

I appreciate doing the extra cleanup work here.

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


More information about the llvm-commits mailing list