From 9bd2aacc68a1c7632abb9410640400dcc09ef50b Mon Sep 17 00:00:00 2001 From: Andrew Rogers Date: Tue, 29 Jul 2025 08:32:07 -0700 Subject: [llvm] annotate recently added interfaces for DLL export (#150101) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## Purpose This patch is one in a series of code-mods that annotate LLVM’s public interface for export. This patch annotates symbols that were recently added to LLVM without proper annotations. The annotations currently have no meaningful impact on the LLVM build; however, they are a prerequisite to support an LLVM Windows DLL (shared library) build. ## Background This effort is tracked in #109483. Additional context is provided in [this discourse](https://discourse.llvm.org/t/psa-annotating-llvm-public-interface/85307), and documentation for `LLVM_ABI` and related annotations is found in the LLVM repo [here](https://github.com/llvm/llvm-project/blob/main/llvm/docs/InterfaceExportAnnotations.rst). ## Overview The bulk of these changes were generated automatically using the [Interface Definition Scanner (IDS)](https://github.com/compnerd/ids) tool, followed formatting with `git clang-format`. The following manual adjustments were also applied after running IDS: - Add `LLVM_EXPORT_TEMPLATE` and `LLVM_TEMPLATE_ABI` annotations to explicitly instantiated instances of `llvm::object::SFrameParser`. ## Validation Local builds and tests to validate cross-platform compatibility. This included llvm, clang, and lldb on the following configurations: - Windows with MSVC - Windows with Clang - Linux with GCC - Linux with Clang - Darwin with Clang --- llvm/lib/Object/SFrameParser.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'llvm/lib/Object/SFrameParser.cpp') diff --git a/llvm/lib/Object/SFrameParser.cpp b/llvm/lib/Object/SFrameParser.cpp index 2d74d1d6b382..6f0037c547c5 100644 --- a/llvm/lib/Object/SFrameParser.cpp +++ b/llvm/lib/Object/SFrameParser.cpp @@ -51,5 +51,6 @@ Expected> SFrameParser::create(ArrayRef Contents) { return SFrameParser(Contents, *Header); } -template class llvm::object::SFrameParser; -template class llvm::object::SFrameParser; +template class LLVM_EXPORT_TEMPLATE llvm::object::SFrameParser; +template class LLVM_EXPORT_TEMPLATE + llvm::object::SFrameParser; -- cgit v1.2.3 From ded255e56ee1f2ef27e85b013f572fca34ca57bc Mon Sep 17 00:00:00 2001 From: Pavel Labath Date: Wed, 30 Jul 2025 11:23:35 +0200 Subject: [Object] Parsing and dumping of SFrame FDEs (#149828) Also known as Function Description Entries. The entries occupy a contiguous piece of the section, so the code is mostly straight-forward. For more information about the SFrame unwind format, see the [specification](https://sourceware.org/binutils/wiki/sframe) and the related [RFC](https://discourse.llvm.org/t/rfc-adding-sframe-support-to-llvm/86900). --- llvm/lib/Object/SFrameParser.cpp | 67 ++++++++++++++++++++++++++++++++++------ 1 file changed, 58 insertions(+), 9 deletions(-) (limited to 'llvm/lib/Object/SFrameParser.cpp') diff --git a/llvm/lib/Object/SFrameParser.cpp b/llvm/lib/Object/SFrameParser.cpp index 6f0037c547c5..5863490634e3 100644 --- a/llvm/lib/Object/SFrameParser.cpp +++ b/llvm/lib/Object/SFrameParser.cpp @@ -10,27 +10,41 @@ #include "llvm/BinaryFormat/SFrame.h" #include "llvm/Object/Error.h" #include "llvm/Support/FormatVariadic.h" +#include "llvm/Support/MathExtras.h" using namespace llvm; using namespace llvm::object; -template -static Expected getDataSliceAs(ArrayRef Data, - uint64_t Offset) { - static_assert(std::is_trivial_v); - if (Data.size() < Offset + sizeof(T)) { +static Expected> +getDataSlice(ArrayRef Data, uint64_t Offset, uint64_t Size) { + uint64_t End = SaturatingAdd(Offset, Size); + // Data.size() cannot be UINT64_MAX, as it would occupy the whole address + // space. + if (End > Data.size()) { return createStringError( formatv("unexpected end of data at offset {0:x} while reading [{1:x}, " "{2:x})", - Data.size(), Offset, Offset + sizeof(T)) + Data.size(), Offset, End) .str(), object_error::unexpected_eof); } - return *reinterpret_cast(Data.data() + Offset); + return Data.slice(Offset, Size); +} + +template +static Expected getDataSliceAs(ArrayRef Data, + uint64_t Offset) { + static_assert(std::is_trivial_v); + Expected> Slice = getDataSlice(Data, Offset, sizeof(T)); + if (!Slice) + return Slice.takeError(); + + return *reinterpret_cast(Slice->data()); } template -Expected> SFrameParser::create(ArrayRef Contents) { +Expected> SFrameParser::create(ArrayRef Contents, + uint64_t SectionAddress) { Expected &> Preamble = getDataSliceAs>(Contents, 0); if (!Preamble) @@ -48,7 +62,42 @@ Expected> SFrameParser::create(ArrayRef Contents) { getDataSliceAs>(Contents, 0); if (!Header) return Header.takeError(); - return SFrameParser(Contents, *Header); + return SFrameParser(Contents, SectionAddress, *Header); +} + +template +Expected> SFrameParser::getAuxHeader() const { + return getDataSlice(Data, sizeof(Header), Header.AuxHdrLen); +} + +template +Expected>> SFrameParser::fdes() const { + Expected> Slice = getDataSlice( + Data, getFDEBase(), Header.NumFDEs * sizeof(sframe::FuncDescEntry)); + if (!Slice) + return Slice.takeError(); + return ArrayRef( + reinterpret_cast *>(Slice->data()), + Header.NumFDEs); +} + +template +uint64_t SFrameParser::getAbsoluteStartAddress( + typename FDERange::iterator FDE) const { + uint64_t Result = SectionAddress + FDE->StartAddress; + + if ((getPreamble().Flags.value() & sframe::Flags::FDEFuncStartPCRel) == + sframe::Flags::FDEFuncStartPCRel) { + uintptr_t DataPtr = reinterpret_cast(Data.data()); + uintptr_t FDEPtr = reinterpret_cast(&*FDE); + + assert(DataPtr <= FDEPtr && FDEPtr < DataPtr + Data.size() && + "Iterator does not belong to this object!"); + + Result += FDEPtr - DataPtr; + } + + return Result; } template class LLVM_EXPORT_TEMPLATE llvm::object::SFrameParser; -- cgit v1.2.3