__  __    __   __  _____      _            _          _____ _          _ _ 
 |  \/  |   \ \ / / |  __ \    (_)          | |        / ____| |        | | |
 | \  / |_ __\ V /  | |__) | __ ___   ____ _| |_ ___  | (___ | |__   ___| | |
 | |\/| | '__|> <   |  ___/ '__| \ \ / / _` | __/ _ \  \___ \| '_ \ / _ \ | |
 | |  | | |_ / . \  | |   | |  | |\ V / (_| | ||  __/  ____) | | | |  __/ | |
 |_|  |_|_(_)_/ \_\ |_|   |_|  |_| \_/ \__,_|\__\___| |_____/|_| |_|\___V 2.1
 if you need WebShell for Seo everyday contact me on Telegram
 Telegram Address : @jackleet
        
        
For_More_Tools: Telegram: @jackleet | Bulk Smtp support mail sender | Business Mail Collector | Mail Bouncer All Mail | Bulk Office Mail Validator | Html Letter private



Upload:

Command:

www-data@216.73.217.146: ~ $
<!DOCTYPE html>

<html lang="en" data-content_root="../">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="viewport" content="width=device-width, initial-scale=1" />

    <title>BPF Type Format (BTF) &#8212; The Linux Kernel  documentation</title>
    <link rel="stylesheet" type="text/css" href="../_static/pygments.css?v=fa44fd50" />
    <link rel="stylesheet" type="text/css" href="../_static/alabaster.css?v=3918102e" />
    <script src="../_static/documentation_options.js?v=5929fcd5"></script>
    <script src="../_static/doctools.js?v=9bcbadda"></script>
    <script src="../_static/sphinx_highlight.js?v=dc90522c"></script>
    <link rel="index" title="Index" href="../genindex.html" />
    <link rel="search" title="Search" href="../search.html" />
    <link rel="next" title="Frequently asked questions (FAQ)" href="faq.html" />
    <link rel="prev" title="1 BPF ABI Recommended Conventions and Guidelines v1.0" href="standardization/abi.html" />
   
  <link rel="stylesheet" href="../_static/custom.css" type="text/css" />
  

  
  

  </head><body>
  <div class="document">
    
      <div class="sphinxsidebar" role="navigation" aria-label="Main">
        <div class="sphinxsidebarwrapper">
            <p class="logo"><a href="../index.html">
              <img class="logo" src="../_static/logo.svg" alt="Logo of The Linux Kernel"/>
            </a></p>
<h1 class="logo"><a href="../index.html">The Linux Kernel</a></h1>



<p class="blurb">6.18.50</p>







<search id="searchbox" style="display: none" role="search">
  <h3 id="searchlabel">Quick search</h3>
    <div class="searchformwrapper">
    <form class="search" action="../search.html" method="get">
      <input type="text" name="q" aria-labelledby="searchlabel" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false"/>
      <input type="submit" value="Go" />
    </form>
    </div>
</search>
<script>document.getElementById('searchbox').style.display = "block"</script>


<p>
<h3 class="kernel-toc-contents">Contents</h3>
<input type="checkbox" class="kernel-toc-toggle" id = "kernel-toc-toggle" checked>
<label class="kernel-toc-title" for="kernel-toc-toggle"></label>

<div class="kerneltoc" id="kerneltoc">
<ul>
<li class="toctree-l1"><a class="reference internal" href="../process/development-process.html">Development process</a></li>
<li class="toctree-l1"><a class="reference internal" href="../process/submitting-patches.html">Submitting patches</a></li>
<li class="toctree-l1"><a class="reference internal" href="../process/code-of-conduct.html">Code of conduct</a></li>
<li class="toctree-l1"><a class="reference internal" href="../maintainer/index.html">Maintainer handbook</a></li>
<li class="toctree-l1"><a class="reference internal" href="../process/index.html">All development-process docs</a></li>
</ul>
<ul class="current">
<li class="toctree-l1"><a class="reference internal" href="../core-api/index.html">Core API</a></li>
<li class="toctree-l1"><a class="reference internal" href="../driver-api/index.html">Driver APIs</a></li>
<li class="toctree-l1 current"><a class="reference internal" href="../subsystem-apis.html">Subsystems</a><ul class="current">
<li class="toctree-l2"><a class="reference internal" href="../subsystem-apis.html#core-subsystems">Core subsystems</a></li>
<li class="toctree-l2"><a class="reference internal" href="../subsystem-apis.html#human-interfaces">Human interfaces</a></li>
<li class="toctree-l2"><a class="reference internal" href="../subsystem-apis.html#networking-interfaces">Networking interfaces</a></li>
<li class="toctree-l2"><a class="reference internal" href="../subsystem-apis.html#storage-interfaces">Storage interfaces</a></li>
<li class="toctree-l2 current"><a class="reference internal" href="../subsystem-apis.html#other-subsystems">Other subsystems</a><ul class="current">
<li class="toctree-l3"><a class="reference internal" href="../accounting/index.html">Accounting</a></li>
<li class="toctree-l3"><a class="reference internal" href="../cpu-freq/index.html">CPUFreq - CPU frequency and voltage scaling code in the Linux(TM) kernel</a></li>
<li class="toctree-l3"><a class="reference internal" href="../edac/index.html">EDAC Subsystem</a></li>
<li class="toctree-l3"><a class="reference internal" href="../fpga/index.html">FPGA</a></li>
<li class="toctree-l3"><a class="reference internal" href="../i2c/index.html">I2C/SMBus Subsystem</a></li>
<li class="toctree-l3"><a class="reference internal" href="../iio/index.html">Industrial I/O</a></li>
<li class="toctree-l3"><a class="reference internal" href="../pcmcia/index.html">PCMCIA</a></li>
<li class="toctree-l3"><a class="reference internal" href="../spi/index.html">Serial Peripheral Interface (SPI)</a></li>
<li class="toctree-l3"><a class="reference internal" href="../w1/index.html">1-Wire Subsystem</a></li>
<li class="toctree-l3"><a class="reference internal" href="../watchdog/index.html">Watchdog Support</a></li>
<li class="toctree-l3"><a class="reference internal" href="../virt/index.html">Virtualization Support</a></li>
<li class="toctree-l3"><a class="reference internal" href="../hwmon/index.html">Hardware Monitoring</a></li>
<li class="toctree-l3"><a class="reference internal" href="../accel/index.html">Compute Accelerators</a></li>
<li class="toctree-l3"><a class="reference internal" href="../security/index.html">Security Documentation</a></li>
<li class="toctree-l3"><a class="reference internal" href="../crypto/index.html">Crypto API</a></li>
<li class="toctree-l3 current"><a class="reference internal" href="index.html">BPF Documentation</a></li>
<li class="toctree-l3"><a class="reference internal" href="../usb/index.html">USB support</a></li>
<li class="toctree-l3"><a class="reference internal" href="../PCI/index.html">PCI Bus Subsystem</a></li>
<li class="toctree-l3"><a class="reference internal" href="../misc-devices/index.html">Assorted Miscellaneous Devices Documentation</a></li>
<li class="toctree-l3"><a class="reference internal" href="../peci/index.html">PECI Subsystem</a></li>
<li class="toctree-l3"><a class="reference internal" href="../wmi/index.html">WMI Subsystem</a></li>
<li class="toctree-l3"><a class="reference internal" href="../tee/index.html">TEE Subsystem</a></li>
</ul>
</li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="../locking/index.html">Locking</a></li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../process/license-rules.html">Licensing rules</a></li>
<li class="toctree-l1"><a class="reference internal" href="../doc-guide/index.html">Writing documentation</a></li>
<li class="toctree-l1"><a class="reference internal" href="../dev-tools/index.html">Development tools</a></li>
<li class="toctree-l1"><a class="reference internal" href="../dev-tools/testing-overview.html">Testing guide</a></li>
<li class="toctree-l1"><a class="reference internal" href="../kernel-hacking/index.html">Hacking guide</a></li>
<li class="toctree-l1"><a class="reference internal" href="../trace/index.html">Tracing</a></li>
<li class="toctree-l1"><a class="reference internal" href="../fault-injection/index.html">Fault injection</a></li>
<li class="toctree-l1"><a class="reference internal" href="../livepatch/index.html">Livepatching</a></li>
<li class="toctree-l1"><a class="reference internal" href="../rust/index.html">Rust</a></li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../admin-guide/index.html">Administration</a></li>
<li class="toctree-l1"><a class="reference internal" href="../kbuild/index.html">Build system</a></li>
<li class="toctree-l1"><a class="reference internal" href="../admin-guide/reporting-issues.html">Reporting issues</a></li>
<li class="toctree-l1"><a class="reference internal" href="../tools/index.html">Userspace tools</a></li>
<li class="toctree-l1"><a class="reference internal" href="../userspace-api/index.html">Userspace API</a></li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../firmware-guide/index.html">Firmware</a></li>
<li class="toctree-l1"><a class="reference internal" href="../devicetree/index.html">Firmware and Devicetree</a></li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../arch/index.html">CPU architectures</a></li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../staging/index.html">Unsorted documentation</a></li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../translations/index.html">Translations</a></li>
</ul>

</div>

<script type="text/javascript"> <!--
  var sbar = document.getElementsByClassName("sphinxsidebar")[0];
  let currents = document.getElementsByClassName("current")
  if (currents.length) {
    sbar.scrollTop = currents[currents.length - 1].offsetTop;
  }
  --> </script>
  <div role="note" aria-label="source link">
    <h3>This Page</h3>
    <ul class="this-page-menu">
      <li><a href="../_sources/bpf/btf.rst.txt"
            rel="nofollow">Show Source</a></li>
    </ul>
   </div>
        </div>
      </div>
      <div class="documentwrapper">
        <div class="bodywrapper">
          

          <div class="body" role="main">
            
  



<section id="bpf-type-format-btf">
<h1>BPF Type Format (BTF)<a class="headerlink" href="#bpf-type-format-btf" title="Link to this heading">¶</a></h1>
<section id="introduction">
<h2>1. Introduction<a class="headerlink" href="#introduction" title="Link to this heading">¶</a></h2>
<p>BTF (BPF Type Format) is the metadata format which encodes the debug info
related to BPF program/map. The name BTF was used initially to describe data
types. The BTF was later extended to include function info for defined
subroutines, and line info for source/line information.</p>
<p>The debug info is used for map pretty print, function signature, etc. The
function signature enables better bpf program/function kernel symbol. The line
info helps generate source annotated translated byte code, jited code and
verifier log.</p>
<dl class="simple">
<dt>The BTF specification contains two parts,</dt><dd><ul class="simple">
<li><p>BTF kernel API</p></li>
<li><p>BTF ELF file format</p></li>
</ul>
</dd>
</dl>
<p>The kernel API is the contract between user space and kernel. The kernel
verifies the BTF info before using it. The ELF file format is a user space
contract between ELF file and libbpf loader.</p>
<p>The type and string sections are part of the BTF kernel API, describing the
debug info (mostly types related) referenced by the bpf program. These two
sections are discussed in details in <a class="reference internal" href="#btf-type-string"><span class="std std-ref">2. BTF Type and String Encoding</span></a>.</p>
</section>
<section id="btf-type-and-string-encoding">
<span id="btf-type-string"></span><h2>2. BTF Type and String Encoding<a class="headerlink" href="#btf-type-and-string-encoding" title="Link to this heading">¶</a></h2>
<p>The file <code class="docutils literal notranslate"><span class="pre">include/uapi/linux/btf.h</span></code> provides high-level definition of how
types/strings are encoded.</p>
<p>The beginning of data blob must be:</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>struct btf_header {
    __u16   magic;
    __u8    version;
    __u8    flags;
    __u32   hdr_len;

    /* All offsets are in bytes relative to the end of this header */
    __u32   type_off;       /* offset of type section       */
    __u32   type_len;       /* length of type section       */
    __u32   str_off;        /* offset of string section     */
    __u32   str_len;        /* length of string section     */
};
</pre></div>
</div>
<p>The magic is <code class="docutils literal notranslate"><span class="pre">0xeB9F</span></code>, which has different encoding for big and little
endian systems, and can be used to test whether BTF is generated for big- or
little-endian target. The <code class="docutils literal notranslate"><span class="pre">btf_header</span></code> is designed to be extensible with
<code class="docutils literal notranslate"><span class="pre">hdr_len</span></code> equal to <code class="docutils literal notranslate"><span class="pre">sizeof(struct</span> <span class="pre">btf_header)</span></code> when a data blob is
generated.</p>
<section id="string-encoding">
<h3>2.1 String Encoding<a class="headerlink" href="#string-encoding" title="Link to this heading">¶</a></h3>
<p>The first string in the string section must be a null string. The rest of
string table is a concatenation of other null-terminated strings.</p>
</section>
<section id="type-encoding">
<h3>2.2 Type Encoding<a class="headerlink" href="#type-encoding" title="Link to this heading">¶</a></h3>
<p>The type id <code class="docutils literal notranslate"><span class="pre">0</span></code> is reserved for <code class="docutils literal notranslate"><span class="pre">void</span></code> type. The type section is parsed
sequentially and type id is assigned to each recognized type starting from id
<code class="docutils literal notranslate"><span class="pre">1</span></code>. Currently, the following types are supported:</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>#define BTF_KIND_INT            1       /* Integer      */
#define BTF_KIND_PTR            2       /* Pointer      */
#define BTF_KIND_ARRAY          3       /* Array        */
#define BTF_KIND_STRUCT         4       /* Struct       */
#define BTF_KIND_UNION          5       /* Union        */
#define BTF_KIND_ENUM           6       /* Enumeration up to 32-bit values */
#define BTF_KIND_FWD            7       /* Forward      */
#define BTF_KIND_TYPEDEF        8       /* Typedef      */
#define BTF_KIND_VOLATILE       9       /* Volatile     */
#define BTF_KIND_CONST          10      /* Const        */
#define BTF_KIND_RESTRICT       11      /* Restrict     */
#define BTF_KIND_FUNC           12      /* Function     */
#define BTF_KIND_FUNC_PROTO     13      /* Function Proto       */
#define BTF_KIND_VAR            14      /* Variable     */
#define BTF_KIND_DATASEC        15      /* Section      */
#define BTF_KIND_FLOAT          16      /* Floating point       */
#define BTF_KIND_DECL_TAG       17      /* Decl Tag     */
#define BTF_KIND_TYPE_TAG       18      /* Type Tag     */
#define BTF_KIND_ENUM64         19      /* Enumeration up to 64-bit values */
</pre></div>
</div>
<p>Note that the type section encodes debug info, not just pure types.
<code class="docutils literal notranslate"><span class="pre">BTF_KIND_FUNC</span></code> is not a type, and it represents a defined subprogram.</p>
<p>Each type contains the following common data:</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>struct btf_type {
    __u32 name_off;
    /* &quot;info&quot; bits arrangement
     * bits  0-15: vlen (e.g. # of struct&#39;s members)
     * bits 16-23: unused
     * bits 24-28: kind (e.g. int, ptr, array...etc)
     * bits 29-30: unused
     * bit     31: kind_flag, currently used by
     *             struct, union, enum, fwd, enum64,
     *             decl_tag and type_tag
     */
    __u32 info;
    /* &quot;size&quot; is used by INT, ENUM, STRUCT, UNION and ENUM64.
     * &quot;size&quot; tells the size of the type it is describing.
     *
     * &quot;type&quot; is used by PTR, TYPEDEF, VOLATILE, CONST, RESTRICT,
     * FUNC, FUNC_PROTO, DECL_TAG and TYPE_TAG.
     * &quot;type&quot; is a type_id referring to another type.
     */
    union {
            __u32 size;
            __u32 type;
    };
};
</pre></div>
</div>
<p>For certain kinds, the common data are followed by kind-specific data. The
<code class="docutils literal notranslate"><span class="pre">name_off</span></code> in <code class="docutils literal notranslate"><span class="pre">struct</span> <span class="pre">btf_type</span></code> specifies the offset in the string table.
The following sections detail encoding of each kind.</p>
<section id="btf-kind-int">
<h4>2.2.1 BTF_KIND_INT<a class="headerlink" href="#btf-kind-int" title="Link to this heading">¶</a></h4>
<dl class="simple">
<dt><code class="docutils literal notranslate"><span class="pre">struct</span> <span class="pre">btf_type</span></code> encoding requirement:</dt><dd><ul class="simple">
<li><p><code class="docutils literal notranslate"><span class="pre">name_off</span></code>: any valid offset</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">info.kind_flag</span></code>: 0</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">info.kind</span></code>: BTF_KIND_INT</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">info.vlen</span></code>: 0</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">size</span></code>: the size of the int type in bytes.</p></li>
</ul>
</dd>
</dl>
<p><code class="docutils literal notranslate"><span class="pre">btf_type</span></code> is followed by a <code class="docutils literal notranslate"><span class="pre">u32</span></code> with the following bits arrangement:</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>#define BTF_INT_ENCODING(VAL)   (((VAL) &amp; 0x0f000000) &gt;&gt; 24)
#define BTF_INT_OFFSET(VAL)     (((VAL) &amp; 0x00ff0000) &gt;&gt; 16)
#define BTF_INT_BITS(VAL)       ((VAL)  &amp; 0x000000ff)
</pre></div>
</div>
<p>The <code class="docutils literal notranslate"><span class="pre">BTF_INT_ENCODING</span></code> has the following attributes:</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>#define BTF_INT_SIGNED  (1 &lt;&lt; 0)
#define BTF_INT_CHAR    (1 &lt;&lt; 1)
#define BTF_INT_BOOL    (1 &lt;&lt; 2)
</pre></div>
</div>
<p>The <code class="docutils literal notranslate"><span class="pre">BTF_INT_ENCODING()</span></code> provides extra information: signedness, char, or
bool, for the int type. The char and bool encoding are mostly useful for
pretty print. At most one encoding can be specified for the int type.</p>
<p>The <code class="docutils literal notranslate"><span class="pre">BTF_INT_BITS()</span></code> specifies the number of actual bits held by this int
type. For example, a 4-bit bitfield encodes <code class="docutils literal notranslate"><span class="pre">BTF_INT_BITS()</span></code> equals to 4.
The <code class="docutils literal notranslate"><span class="pre">btf_type.size</span> <span class="pre">*</span> <span class="pre">8</span></code> must be equal to or greater than <code class="docutils literal notranslate"><span class="pre">BTF_INT_BITS()</span></code>
for the type. The maximum value of <code class="docutils literal notranslate"><span class="pre">BTF_INT_BITS()</span></code> is 128.</p>
<p>The <code class="docutils literal notranslate"><span class="pre">BTF_INT_OFFSET()</span></code> specifies the starting bit offset to calculate values
for this int. For example, a bitfield <code class="xref c c-struct broken_xref docutils literal notranslate"><span class="pre">struct</span> <span class="pre">member</span></code> has:</p>
<blockquote>
<div><ul class="simple">
<li><p>btf member bit offset 100 from the start of the structure,</p></li>
<li><p>btf member pointing to an int type,</p></li>
<li><p>the int type has <code class="docutils literal notranslate"><span class="pre">BTF_INT_OFFSET()</span> <span class="pre">=</span> <span class="pre">2</span></code> and <code class="docutils literal notranslate"><span class="pre">BTF_INT_BITS()</span> <span class="pre">=</span> <span class="pre">4</span></code></p></li>
</ul>
</div></blockquote>
<p>Then in the <code class="xref c c-struct broken_xref docutils literal notranslate"><span class="pre">struct</span> <span class="pre">memory</span></code> layout, this member will occupy <code class="docutils literal notranslate"><span class="pre">4</span></code> bits starting
from bits <code class="docutils literal notranslate"><span class="pre">100</span> <span class="pre">+</span> <span class="pre">2</span> <span class="pre">=</span> <span class="pre">102</span></code>.</p>
<p>Alternatively, the bitfield <code class="xref c c-struct broken_xref docutils literal notranslate"><span class="pre">struct</span> <span class="pre">member</span></code> can be the following to access the
same bits as the above:</p>
<blockquote>
<div><ul class="simple">
<li><p>btf member bit offset 102,</p></li>
<li><p>btf member pointing to an int type,</p></li>
<li><p>the int type has <code class="docutils literal notranslate"><span class="pre">BTF_INT_OFFSET()</span> <span class="pre">=</span> <span class="pre">0</span></code> and <code class="docutils literal notranslate"><span class="pre">BTF_INT_BITS()</span> <span class="pre">=</span> <span class="pre">4</span></code></p></li>
</ul>
</div></blockquote>
<p>The original intention of <code class="docutils literal notranslate"><span class="pre">BTF_INT_OFFSET()</span></code> is to provide flexibility of
bitfield encoding. Currently, both llvm and pahole generate
<code class="docutils literal notranslate"><span class="pre">BTF_INT_OFFSET()</span> <span class="pre">=</span> <span class="pre">0</span></code> for all int types.</p>
</section>
<section id="btf-kind-ptr">
<h4>2.2.2 BTF_KIND_PTR<a class="headerlink" href="#btf-kind-ptr" title="Link to this heading">¶</a></h4>
<dl class="simple">
<dt><code class="docutils literal notranslate"><span class="pre">struct</span> <span class="pre">btf_type</span></code> encoding requirement:</dt><dd><ul class="simple">
<li><p><code class="docutils literal notranslate"><span class="pre">name_off</span></code>: 0</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">info.kind_flag</span></code>: 0</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">info.kind</span></code>: BTF_KIND_PTR</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">info.vlen</span></code>: 0</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">type</span></code>: the pointee type of the pointer</p></li>
</ul>
</dd>
</dl>
<p>No additional type data follow <code class="docutils literal notranslate"><span class="pre">btf_type</span></code>.</p>
</section>
<section id="btf-kind-array">
<h4>2.2.3 BTF_KIND_ARRAY<a class="headerlink" href="#btf-kind-array" title="Link to this heading">¶</a></h4>
<dl class="simple">
<dt><code class="docutils literal notranslate"><span class="pre">struct</span> <span class="pre">btf_type</span></code> encoding requirement:</dt><dd><ul class="simple">
<li><p><code class="docutils literal notranslate"><span class="pre">name_off</span></code>: 0</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">info.kind_flag</span></code>: 0</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">info.kind</span></code>: BTF_KIND_ARRAY</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">info.vlen</span></code>: 0</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">size/type</span></code>: 0, not used</p></li>
</ul>
</dd>
</dl>
<p><code class="docutils literal notranslate"><span class="pre">btf_type</span></code> is followed by one <code class="docutils literal notranslate"><span class="pre">struct</span> <span class="pre">btf_array</span></code>:</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>struct btf_array {
    __u32   type;
    __u32   index_type;
    __u32   nelems;
};
</pre></div>
</div>
<dl class="simple">
<dt>The <code class="docutils literal notranslate"><span class="pre">struct</span> <span class="pre">btf_array</span></code> encoding:</dt><dd><ul class="simple">
<li><p><code class="docutils literal notranslate"><span class="pre">type</span></code>: the element type</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">index_type</span></code>: the index type</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">nelems</span></code>: the number of elements for this array (<code class="docutils literal notranslate"><span class="pre">0</span></code> is also allowed).</p></li>
</ul>
</dd>
</dl>
<p>The <code class="docutils literal notranslate"><span class="pre">index_type</span></code> can be any regular int type (<code class="docutils literal notranslate"><span class="pre">u8</span></code>, <code class="docutils literal notranslate"><span class="pre">u16</span></code>, <code class="docutils literal notranslate"><span class="pre">u32</span></code>,
<code class="docutils literal notranslate"><span class="pre">u64</span></code>, <code class="docutils literal notranslate"><span class="pre">unsigned</span> <span class="pre">__int128</span></code>). The original design of including
<code class="docutils literal notranslate"><span class="pre">index_type</span></code> follows DWARF, which has an <code class="docutils literal notranslate"><span class="pre">index_type</span></code> for its array type.
Currently in BTF, beyond type verification, the <code class="docutils literal notranslate"><span class="pre">index_type</span></code> is not used.</p>
<p>The <code class="docutils literal notranslate"><span class="pre">struct</span> <span class="pre">btf_array</span></code> allows chaining through element type to represent
multidimensional arrays. For example, for <code class="docutils literal notranslate"><span class="pre">int</span> <span class="pre">a[5][6]</span></code>, the following type
information illustrates the chaining:</p>
<blockquote>
<div><ul class="simple">
<li><p>[1]: int</p></li>
<li><p>[2]: array, <code class="docutils literal notranslate"><span class="pre">btf_array.type</span> <span class="pre">=</span> <span class="pre">[1]</span></code>, <code class="docutils literal notranslate"><span class="pre">btf_array.nelems</span> <span class="pre">=</span> <span class="pre">6</span></code></p></li>
<li><p>[3]: array, <code class="docutils literal notranslate"><span class="pre">btf_array.type</span> <span class="pre">=</span> <span class="pre">[2]</span></code>, <code class="docutils literal notranslate"><span class="pre">btf_array.nelems</span> <span class="pre">=</span> <span class="pre">5</span></code></p></li>
</ul>
</div></blockquote>
<p>Currently, both pahole and llvm collapse multidimensional array into
one-dimensional array, e.g., for <code class="docutils literal notranslate"><span class="pre">a[5][6]</span></code>, the <code class="docutils literal notranslate"><span class="pre">btf_array.nelems</span></code> is
equal to <code class="docutils literal notranslate"><span class="pre">30</span></code>. This is because the original use case is map pretty print
where the whole array is dumped out so one-dimensional array is enough. As
more BTF usage is explored, pahole and llvm can be changed to generate proper
chained representation for multidimensional arrays.</p>
</section>
<section id="btf-kind-struct">
<h4>2.2.4 BTF_KIND_STRUCT<a class="headerlink" href="#btf-kind-struct" title="Link to this heading">¶</a></h4>
</section>
<section id="btf-kind-union">
<h4>2.2.5 BTF_KIND_UNION<a class="headerlink" href="#btf-kind-union" title="Link to this heading">¶</a></h4>
<dl class="simple">
<dt><code class="docutils literal notranslate"><span class="pre">struct</span> <span class="pre">btf_type</span></code> encoding requirement:</dt><dd><ul class="simple">
<li><p><code class="docutils literal notranslate"><span class="pre">name_off</span></code>: 0 or offset to a valid C identifier</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">info.kind_flag</span></code>: 0 or 1</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">info.kind</span></code>: BTF_KIND_STRUCT or BTF_KIND_UNION</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">info.vlen</span></code>: the number of struct/<code class="xref c c-union broken_xref docutils literal notranslate"><span class="pre">union</span> <span class="pre">members</span></code></p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">info.size</span></code>: the size of the struct/<code class="xref c c-union broken_xref docutils literal notranslate"><span class="pre">union</span> <span class="pre">in</span></code> bytes</p></li>
</ul>
</dd>
</dl>
<p><code class="docutils literal notranslate"><span class="pre">btf_type</span></code> is followed by <code class="docutils literal notranslate"><span class="pre">info.vlen</span></code> number of <code class="docutils literal notranslate"><span class="pre">struct</span> <span class="pre">btf_member</span></code>.:</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>struct btf_member {
    __u32   name_off;
    __u32   type;
    __u32   offset;
};
</pre></div>
</div>
<dl class="simple">
<dt><code class="docutils literal notranslate"><span class="pre">struct</span> <span class="pre">btf_member</span></code> encoding:</dt><dd><ul class="simple">
<li><p><code class="docutils literal notranslate"><span class="pre">name_off</span></code>: offset to a valid C identifier</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">type</span></code>: the member type</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">offset</span></code>: &lt;see below&gt;</p></li>
</ul>
</dd>
</dl>
<p>If the type info <code class="docutils literal notranslate"><span class="pre">kind_flag</span></code> is not set, the offset contains only bit offset
of the member. Note that the base type of the bitfield can only be int or <code class="xref c c-enum broken_xref docutils literal notranslate"><span class="pre">enum</span>
<span class="pre">type</span></code>. If the bitfield size is 32, the base type can be either int or <code class="xref c c-enum broken_xref docutils literal notranslate"><span class="pre">enum</span>
<span class="pre">type</span></code>. If the bitfield size is not 32, the base type must be int, and int type
<code class="docutils literal notranslate"><span class="pre">BTF_INT_BITS()</span></code> encodes the bitfield size.</p>
<p>If the <code class="docutils literal notranslate"><span class="pre">kind_flag</span></code> is set, the <code class="docutils literal notranslate"><span class="pre">btf_member.offset</span></code> contains both member
bitfield size and bit offset. The bitfield size and bit offset are calculated
as below.:</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>#define BTF_MEMBER_BITFIELD_SIZE(val)   ((val) &gt;&gt; 24)
#define BTF_MEMBER_BIT_OFFSET(val)      ((val) &amp; 0xffffff)
</pre></div>
</div>
<p>In this case, if the base type is an int type, it must be a regular int type:</p>
<blockquote>
<div><ul class="simple">
<li><p><code class="docutils literal notranslate"><span class="pre">BTF_INT_OFFSET()</span></code> must be 0.</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">BTF_INT_BITS()</span></code> must be equal to <code class="docutils literal notranslate"><span class="pre">{1,2,4,8,16}</span> <span class="pre">*</span> <span class="pre">8</span></code>.</p></li>
</ul>
</div></blockquote>
<p><a class="reference external" href="https://git.kernel.org/torvalds/c/9d5f9f701b18">Commit 9d5f9f701b18</a> introduced <code class="docutils literal notranslate"><span class="pre">kind_flag</span></code> and explains why both modes
exist.</p>
</section>
<section id="btf-kind-enum">
<h4>2.2.6 BTF_KIND_ENUM<a class="headerlink" href="#btf-kind-enum" title="Link to this heading">¶</a></h4>
<dl class="simple">
<dt><code class="docutils literal notranslate"><span class="pre">struct</span> <span class="pre">btf_type</span></code> encoding requirement:</dt><dd><ul class="simple">
<li><p><code class="docutils literal notranslate"><span class="pre">name_off</span></code>: 0 or offset to a valid C identifier</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">info.kind_flag</span></code>: 0 for unsigned, 1 for signed</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">info.kind</span></code>: BTF_KIND_ENUM</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">info.vlen</span></code>: number of <code class="xref c c-enum broken_xref docutils literal notranslate"><span class="pre">enum</span> <span class="pre">values</span></code></p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">size</span></code>: 1/2/4/8</p></li>
</ul>
</dd>
</dl>
<p><code class="docutils literal notranslate"><span class="pre">btf_type</span></code> is followed by <code class="docutils literal notranslate"><span class="pre">info.vlen</span></code> number of <code class="docutils literal notranslate"><span class="pre">struct</span> <span class="pre">btf_enum</span></code>.:</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>struct btf_enum {
    __u32   name_off;
    __s32   val;
};
</pre></div>
</div>
<dl class="simple">
<dt>The <code class="docutils literal notranslate"><span class="pre">btf_enum</span></code> encoding:</dt><dd><ul class="simple">
<li><p><code class="docutils literal notranslate"><span class="pre">name_off</span></code>: offset to a valid C identifier</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">val</span></code>: any value</p></li>
</ul>
</dd>
</dl>
<p>If the original <code class="xref c c-enum broken_xref docutils literal notranslate"><span class="pre">enum</span> <span class="pre">value</span></code> is signed and the size is less than 4,
that value will be sign extended into 4 bytes. If the size is 8,
the value will be truncated into 4 bytes.</p>
</section>
<section id="btf-kind-fwd">
<h4>2.2.7 BTF_KIND_FWD<a class="headerlink" href="#btf-kind-fwd" title="Link to this heading">¶</a></h4>
<dl class="simple">
<dt><code class="docutils literal notranslate"><span class="pre">struct</span> <span class="pre">btf_type</span></code> encoding requirement:</dt><dd><ul class="simple">
<li><p><code class="docutils literal notranslate"><span class="pre">name_off</span></code>: offset to a valid C identifier</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">info.kind_flag</span></code>: 0 for struct, 1 for union</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">info.kind</span></code>: BTF_KIND_FWD</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">info.vlen</span></code>: 0</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">type</span></code>: 0</p></li>
</ul>
</dd>
</dl>
<p>No additional type data follow <code class="docutils literal notranslate"><span class="pre">btf_type</span></code>.</p>
</section>
<section id="btf-kind-typedef">
<h4>2.2.8 BTF_KIND_TYPEDEF<a class="headerlink" href="#btf-kind-typedef" title="Link to this heading">¶</a></h4>
<dl class="simple">
<dt><code class="docutils literal notranslate"><span class="pre">struct</span> <span class="pre">btf_type</span></code> encoding requirement:</dt><dd><ul class="simple">
<li><p><code class="docutils literal notranslate"><span class="pre">name_off</span></code>: offset to a valid C identifier</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">info.kind_flag</span></code>: 0</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">info.kind</span></code>: BTF_KIND_TYPEDEF</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">info.vlen</span></code>: 0</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">type</span></code>: the type which can be referred by name at <code class="docutils literal notranslate"><span class="pre">name_off</span></code></p></li>
</ul>
</dd>
</dl>
<p>No additional type data follow <code class="docutils literal notranslate"><span class="pre">btf_type</span></code>.</p>
</section>
<section id="btf-kind-volatile">
<h4>2.2.9 BTF_KIND_VOLATILE<a class="headerlink" href="#btf-kind-volatile" title="Link to this heading">¶</a></h4>
<dl class="simple">
<dt><code class="docutils literal notranslate"><span class="pre">struct</span> <span class="pre">btf_type</span></code> encoding requirement:</dt><dd><ul class="simple">
<li><p><code class="docutils literal notranslate"><span class="pre">name_off</span></code>: 0</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">info.kind_flag</span></code>: 0</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">info.kind</span></code>: BTF_KIND_VOLATILE</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">info.vlen</span></code>: 0</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">type</span></code>: the type with <code class="docutils literal notranslate"><span class="pre">volatile</span></code> qualifier</p></li>
</ul>
</dd>
</dl>
<p>No additional type data follow <code class="docutils literal notranslate"><span class="pre">btf_type</span></code>.</p>
</section>
<section id="btf-kind-const">
<h4>2.2.10 BTF_KIND_CONST<a class="headerlink" href="#btf-kind-const" title="Link to this heading">¶</a></h4>
<dl class="simple">
<dt><code class="docutils literal notranslate"><span class="pre">struct</span> <span class="pre">btf_type</span></code> encoding requirement:</dt><dd><ul class="simple">
<li><p><code class="docutils literal notranslate"><span class="pre">name_off</span></code>: 0</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">info.kind_flag</span></code>: 0</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">info.kind</span></code>: BTF_KIND_CONST</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">info.vlen</span></code>: 0</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">type</span></code>: the type with <code class="docutils literal notranslate"><span class="pre">const</span></code> qualifier</p></li>
</ul>
</dd>
</dl>
<p>No additional type data follow <code class="docutils literal notranslate"><span class="pre">btf_type</span></code>.</p>
</section>
<section id="btf-kind-restrict">
<h4>2.2.11 BTF_KIND_RESTRICT<a class="headerlink" href="#btf-kind-restrict" title="Link to this heading">¶</a></h4>
<dl class="simple">
<dt><code class="docutils literal notranslate"><span class="pre">struct</span> <span class="pre">btf_type</span></code> encoding requirement:</dt><dd><ul class="simple">
<li><p><code class="docutils literal notranslate"><span class="pre">name_off</span></code>: 0</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">info.kind_flag</span></code>: 0</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">info.kind</span></code>: BTF_KIND_RESTRICT</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">info.vlen</span></code>: 0</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">type</span></code>: the type with <code class="docutils literal notranslate"><span class="pre">restrict</span></code> qualifier</p></li>
</ul>
</dd>
</dl>
<p>No additional type data follow <code class="docutils literal notranslate"><span class="pre">btf_type</span></code>.</p>
</section>
<section id="btf-kind-func">
<h4>2.2.12 BTF_KIND_FUNC<a class="headerlink" href="#btf-kind-func" title="Link to this heading">¶</a></h4>
<dl class="simple">
<dt><code class="docutils literal notranslate"><span class="pre">struct</span> <span class="pre">btf_type</span></code> encoding requirement:</dt><dd><ul class="simple">
<li><p><code class="docutils literal notranslate"><span class="pre">name_off</span></code>: offset to a valid C identifier</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">info.kind_flag</span></code>: 0</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">info.kind</span></code>: BTF_KIND_FUNC</p></li>
<li><dl class="simple">
<dt><code class="docutils literal notranslate"><span class="pre">info.vlen</span></code>: linkage information (BTF_FUNC_STATIC, BTF_FUNC_GLOBAL</dt><dd><p>or BTF_FUNC_EXTERN - see <a class="reference internal" href="#btf-function-linkage-constants"><span class="std std-ref">2.3.1 Function Linkage Constant Values</span></a>)</p>
</dd>
</dl>
</li>
<li><p><code class="docutils literal notranslate"><span class="pre">type</span></code>: a BTF_KIND_FUNC_PROTO type</p></li>
</ul>
</dd>
</dl>
<p>No additional type data follow <code class="docutils literal notranslate"><span class="pre">btf_type</span></code>.</p>
<p>A BTF_KIND_FUNC defines not a type, but a subprogram (function) whose
signature is defined by <code class="docutils literal notranslate"><span class="pre">type</span></code>. The subprogram is thus an instance of that
type. The BTF_KIND_FUNC may in turn be referenced by a func_info in the
<a class="reference internal" href="#btf-ext-section"><span class="std std-ref">4.2 .BTF.ext section</span></a> (ELF) or in the arguments to <a class="reference internal" href="#bpf-prog-load"><span class="std std-ref">3.3 BPF_PROG_LOAD</span></a>
(ABI).</p>
<p>Currently, only linkage values of BTF_FUNC_STATIC and BTF_FUNC_GLOBAL are
supported in the kernel.</p>
</section>
<section id="btf-kind-func-proto">
<h4>2.2.13 BTF_KIND_FUNC_PROTO<a class="headerlink" href="#btf-kind-func-proto" title="Link to this heading">¶</a></h4>
<dl class="simple">
<dt><code class="docutils literal notranslate"><span class="pre">struct</span> <span class="pre">btf_type</span></code> encoding requirement:</dt><dd><ul class="simple">
<li><p><code class="docutils literal notranslate"><span class="pre">name_off</span></code>: 0</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">info.kind_flag</span></code>: 0</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">info.kind</span></code>: BTF_KIND_FUNC_PROTO</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">info.vlen</span></code>: # of parameters</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">type</span></code>: the return type</p></li>
</ul>
</dd>
</dl>
<p><code class="docutils literal notranslate"><span class="pre">btf_type</span></code> is followed by <code class="docutils literal notranslate"><span class="pre">info.vlen</span></code> number of <code class="docutils literal notranslate"><span class="pre">struct</span> <span class="pre">btf_param</span></code>.:</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>struct btf_param {
    __u32   name_off;
    __u32   type;
};
</pre></div>
</div>
<p>If a BTF_KIND_FUNC_PROTO type is referred by a BTF_KIND_FUNC type, then
<code class="docutils literal notranslate"><span class="pre">btf_param.name_off</span></code> must point to a valid C identifier except for the
possible last argument representing the variable argument. The btf_param.type
refers to parameter type.</p>
<p>If the function has variable arguments, the last parameter is encoded with
<code class="docutils literal notranslate"><span class="pre">name_off</span> <span class="pre">=</span> <span class="pre">0</span></code> and <code class="docutils literal notranslate"><span class="pre">type</span> <span class="pre">=</span> <span class="pre">0</span></code>.</p>
</section>
<section id="btf-kind-var">
<h4>2.2.14 BTF_KIND_VAR<a class="headerlink" href="#btf-kind-var" title="Link to this heading">¶</a></h4>
<dl class="simple">
<dt><code class="docutils literal notranslate"><span class="pre">struct</span> <span class="pre">btf_type</span></code> encoding requirement:</dt><dd><ul class="simple">
<li><p><code class="docutils literal notranslate"><span class="pre">name_off</span></code>: offset to a valid C identifier</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">info.kind_flag</span></code>: 0</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">info.kind</span></code>: BTF_KIND_VAR</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">info.vlen</span></code>: 0</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">type</span></code>: the type of the variable</p></li>
</ul>
</dd>
</dl>
<p><code class="docutils literal notranslate"><span class="pre">btf_type</span></code> is followed by a single <code class="docutils literal notranslate"><span class="pre">struct</span> <span class="pre">btf_variable</span></code> with the
following data:</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>struct btf_var {
    __u32   linkage;
};
</pre></div>
</div>
<p><code class="docutils literal notranslate"><span class="pre">btf_var.linkage</span></code> may take the values: BTF_VAR_STATIC, BTF_VAR_GLOBAL_ALLOCATED or BTF_VAR_GLOBAL_EXTERN -
see <a class="reference internal" href="#btf-var-linkage-constants"><span class="std std-ref">2.3.2 Variable Linkage Constant Values</span></a>.</p>
<p>Not all type of global variables are supported by LLVM at this point.
The following is currently available:</p>
<blockquote>
<div><ul class="simple">
<li><p>static variables with or without section attributes</p></li>
<li><p>global variables with section attributes</p></li>
</ul>
</div></blockquote>
<p>The latter is for future extraction of map key/value type id’s from a
map definition.</p>
</section>
<section id="btf-kind-datasec">
<h4>2.2.15 BTF_KIND_DATASEC<a class="headerlink" href="#btf-kind-datasec" title="Link to this heading">¶</a></h4>
<dl class="simple">
<dt><code class="docutils literal notranslate"><span class="pre">struct</span> <span class="pre">btf_type</span></code> encoding requirement:</dt><dd><ul class="simple">
<li><dl class="simple">
<dt><code class="docutils literal notranslate"><span class="pre">name_off</span></code>: offset to a valid name associated with a variable or</dt><dd><p>one of .data/.bss/.rodata</p>
</dd>
</dl>
</li>
<li><p><code class="docutils literal notranslate"><span class="pre">info.kind_flag</span></code>: 0</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">info.kind</span></code>: BTF_KIND_DATASEC</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">info.vlen</span></code>: # of variables</p></li>
<li><dl class="simple">
<dt><code class="docutils literal notranslate"><span class="pre">size</span></code>: total section size in bytes (0 at compilation time, patched</dt><dd><p>to actual size by BPF loaders such as libbpf)</p>
</dd>
</dl>
</li>
</ul>
</dd>
</dl>
<p><code class="docutils literal notranslate"><span class="pre">btf_type</span></code> is followed by <code class="docutils literal notranslate"><span class="pre">info.vlen</span></code> number of <code class="docutils literal notranslate"><span class="pre">struct</span> <span class="pre">btf_var_secinfo</span></code>.:</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>struct btf_var_secinfo {
    __u32   type;
    __u32   offset;
    __u32   size;
};
</pre></div>
</div>
<dl class="simple">
<dt><code class="docutils literal notranslate"><span class="pre">struct</span> <span class="pre">btf_var_secinfo</span></code> encoding:</dt><dd><ul class="simple">
<li><p><code class="docutils literal notranslate"><span class="pre">type</span></code>: the type of the BTF_KIND_VAR variable</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">offset</span></code>: the in-section offset of the variable</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">size</span></code>: the size of the variable in bytes</p></li>
</ul>
</dd>
</dl>
</section>
<section id="btf-kind-float">
<h4>2.2.16 BTF_KIND_FLOAT<a class="headerlink" href="#btf-kind-float" title="Link to this heading">¶</a></h4>
<dl class="simple">
<dt><code class="docutils literal notranslate"><span class="pre">struct</span> <span class="pre">btf_type</span></code> encoding requirement:</dt><dd><ul class="simple">
<li><p><code class="docutils literal notranslate"><span class="pre">name_off</span></code>: any valid offset</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">info.kind_flag</span></code>: 0</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">info.kind</span></code>: BTF_KIND_FLOAT</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">info.vlen</span></code>: 0</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">size</span></code>: the size of the float type in bytes: 2, 4, 8, 12 or 16.</p></li>
</ul>
</dd>
</dl>
<p>No additional type data follow <code class="docutils literal notranslate"><span class="pre">btf_type</span></code>.</p>
</section>
<section id="btf-kind-decl-tag">
<h4>2.2.17 BTF_KIND_DECL_TAG<a class="headerlink" href="#btf-kind-decl-tag" title="Link to this heading">¶</a></h4>
<dl class="simple">
<dt><code class="docutils literal notranslate"><span class="pre">struct</span> <span class="pre">btf_type</span></code> encoding requirement:</dt><dd><ul class="simple">
<li><p><code class="docutils literal notranslate"><span class="pre">name_off</span></code>: offset to a non-empty string</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">info.kind_flag</span></code>: 0 or 1</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">info.kind</span></code>: BTF_KIND_DECL_TAG</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">info.vlen</span></code>: 0</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">type</span></code>: <code class="docutils literal notranslate"><span class="pre">struct</span></code>, <code class="docutils literal notranslate"><span class="pre">union</span></code>, <code class="docutils literal notranslate"><span class="pre">func</span></code>, <code class="docutils literal notranslate"><span class="pre">var</span></code> or <code class="docutils literal notranslate"><span class="pre">typedef</span></code></p></li>
</ul>
</dd>
</dl>
<p><code class="docutils literal notranslate"><span class="pre">btf_type</span></code> is followed by <code class="docutils literal notranslate"><span class="pre">struct</span> <span class="pre">btf_decl_tag</span></code>.:</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>struct btf_decl_tag {
    __u32   component_idx;
};
</pre></div>
</div>
<p>The <code class="docutils literal notranslate"><span class="pre">type</span></code> should be <code class="docutils literal notranslate"><span class="pre">struct</span></code>, <code class="docutils literal notranslate"><span class="pre">union</span></code>, <code class="docutils literal notranslate"><span class="pre">func</span></code>, <code class="docutils literal notranslate"><span class="pre">var</span></code> or <code class="docutils literal notranslate"><span class="pre">typedef</span></code>.
For <code class="docutils literal notranslate"><span class="pre">var</span></code> or <code class="docutils literal notranslate"><span class="pre">typedef</span></code> type, <code class="docutils literal notranslate"><span class="pre">btf_decl_tag.component_idx</span></code> must be <code class="docutils literal notranslate"><span class="pre">-1</span></code>.
For the other three types, if the btf_decl_tag attribute is
applied to the <code class="docutils literal notranslate"><span class="pre">struct</span></code>, <code class="docutils literal notranslate"><span class="pre">union</span></code> or <code class="docutils literal notranslate"><span class="pre">func</span></code> itself,
<code class="docutils literal notranslate"><span class="pre">btf_decl_tag.component_idx</span></code> must be <code class="docutils literal notranslate"><span class="pre">-1</span></code>. Otherwise,
the attribute is applied to a <code class="docutils literal notranslate"><span class="pre">struct</span></code>/<code class="docutils literal notranslate"><span class="pre">union</span></code> member or
a <code class="docutils literal notranslate"><span class="pre">func</span></code> argument, and <code class="docutils literal notranslate"><span class="pre">btf_decl_tag.component_idx</span></code> should be a
valid index (starting from 0) pointing to a member or an argument.</p>
<p>If <code class="docutils literal notranslate"><span class="pre">info.kind_flag</span></code> is 0, then this is a normal decl tag, and the
<code class="docutils literal notranslate"><span class="pre">name_off</span></code> encodes btf_decl_tag attribute string.</p>
<p>If <code class="docutils literal notranslate"><span class="pre">info.kind_flag</span></code> is 1, then the decl tag represents an arbitrary
__attribute__. In this case, <code class="docutils literal notranslate"><span class="pre">name_off</span></code> encodes a string
representing the attribute-list of the attribute specifier. For
example, for an <code class="docutils literal notranslate"><span class="pre">__attribute__((aligned(4)))</span></code> the string’s contents
is <code class="docutils literal notranslate"><span class="pre">aligned(4)</span></code>.</p>
</section>
<section id="btf-kind-type-tag">
<h4>2.2.18 BTF_KIND_TYPE_TAG<a class="headerlink" href="#btf-kind-type-tag" title="Link to this heading">¶</a></h4>
<dl class="simple">
<dt><code class="docutils literal notranslate"><span class="pre">struct</span> <span class="pre">btf_type</span></code> encoding requirement:</dt><dd><ul class="simple">
<li><p><code class="docutils literal notranslate"><span class="pre">name_off</span></code>: offset to a non-empty string</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">info.kind_flag</span></code>: 0 or 1</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">info.kind</span></code>: BTF_KIND_TYPE_TAG</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">info.vlen</span></code>: 0</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">type</span></code>: the type with <code class="docutils literal notranslate"><span class="pre">btf_type_tag</span></code> attribute</p></li>
</ul>
</dd>
</dl>
<p>Currently, <code class="docutils literal notranslate"><span class="pre">BTF_KIND_TYPE_TAG</span></code> is only emitted for pointer types.
It has the following btf type chain:</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>ptr -&gt; [type_tag]*
    -&gt; [const | volatile | restrict | typedef]*
    -&gt; base_type
</pre></div>
</div>
<p>Basically, a pointer type points to zero or more
type_tag, then zero or more const/volatile/restrict/<code class="xref c c-type broken_xref docutils literal notranslate"><span class="pre">typedef</span>
<span class="pre">and</span></code> finally the base type. The base type is one of
int, ptr, array, struct, union, enum, func_proto and float types.</p>
<p>Similarly to decl tags, if the <code class="docutils literal notranslate"><span class="pre">info.kind_flag</span></code> is 0, then this is a
normal type tag, and the <code class="docutils literal notranslate"><span class="pre">name_off</span></code> encodes btf_type_tag attribute
string.</p>
<p>If <code class="docutils literal notranslate"><span class="pre">info.kind_flag</span></code> is 1, then the type tag represents an arbitrary
__attribute__, and the <code class="docutils literal notranslate"><span class="pre">name_off</span></code> encodes a string representing the
attribute-list of the attribute specifier.</p>
</section>
<section id="btf-kind-enum64">
<h4>2.2.19 BTF_KIND_ENUM64<a class="headerlink" href="#btf-kind-enum64" title="Link to this heading">¶</a></h4>
<dl class="simple">
<dt><code class="docutils literal notranslate"><span class="pre">struct</span> <span class="pre">btf_type</span></code> encoding requirement:</dt><dd><ul class="simple">
<li><p><code class="docutils literal notranslate"><span class="pre">name_off</span></code>: 0 or offset to a valid C identifier</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">info.kind_flag</span></code>: 0 for unsigned, 1 for signed</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">info.kind</span></code>: BTF_KIND_ENUM64</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">info.vlen</span></code>: number of <code class="xref c c-enum broken_xref docutils literal notranslate"><span class="pre">enum</span> <span class="pre">values</span></code></p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">size</span></code>: 1/2/4/8</p></li>
</ul>
</dd>
</dl>
<p><code class="docutils literal notranslate"><span class="pre">btf_type</span></code> is followed by <code class="docutils literal notranslate"><span class="pre">info.vlen</span></code> number of <code class="docutils literal notranslate"><span class="pre">struct</span> <span class="pre">btf_enum64</span></code>.:</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>struct btf_enum64 {
    __u32   name_off;
    __u32   val_lo32;
    __u32   val_hi32;
};
</pre></div>
</div>
<dl class="simple">
<dt>The <code class="docutils literal notranslate"><span class="pre">btf_enum64</span></code> encoding:</dt><dd><ul class="simple">
<li><p><code class="docutils literal notranslate"><span class="pre">name_off</span></code>: offset to a valid C identifier</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">val_lo32</span></code>: lower 32-bit value for a 64-bit value</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">val_hi32</span></code>: high 32-bit value for a 64-bit value</p></li>
</ul>
</dd>
</dl>
<p>If the original <code class="xref c c-enum broken_xref docutils literal notranslate"><span class="pre">enum</span> <span class="pre">value</span></code> is signed and the size is less than 8,
that value will be sign extended into 8 bytes.</p>
</section>
</section>
<section id="constant-values">
<h3>2.3 Constant Values<a class="headerlink" href="#constant-values" title="Link to this heading">¶</a></h3>
<section id="function-linkage-constant-values">
<span id="btf-function-linkage-constants"></span><h4>2.3.1 Function Linkage Constant Values<a class="headerlink" href="#function-linkage-constant-values" title="Link to this heading">¶</a></h4>
<table class="docutils align-default" id="id3">
<caption><span class="caption-text">Function Linkage Values and Meanings</span><a class="headerlink" href="#id3" title="Link to this table">¶</a></caption>
<thead>
<tr class="row-odd"><th class="head"><p>kind</p></th>
<th class="head"><p>value</p></th>
<th class="head"><p>description</p></th>
</tr>
</thead>
<tbody>
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">BTF_FUNC_STATIC</span></code></p></td>
<td><p>0x0</p></td>
<td><p>definition of subprogram not visible outside containing compilation unit</p></td>
</tr>
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">BTF_FUNC_GLOBAL</span></code></p></td>
<td><p>0x1</p></td>
<td><p>definition of subprogram visible outside containing compilation unit</p></td>
</tr>
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">BTF_FUNC_EXTERN</span></code></p></td>
<td><p>0x2</p></td>
<td><p>declaration of a subprogram whose definition is outside the containing compilation unit</p></td>
</tr>
</tbody>
</table>
</section>
<section id="variable-linkage-constant-values">
<span id="btf-var-linkage-constants"></span><h4>2.3.2 Variable Linkage Constant Values<a class="headerlink" href="#variable-linkage-constant-values" title="Link to this heading">¶</a></h4>
<table class="docutils align-default" id="id4">
<caption><span class="caption-text">Variable Linkage Values and Meanings</span><a class="headerlink" href="#id4" title="Link to this table">¶</a></caption>
<thead>
<tr class="row-odd"><th class="head"><p>kind</p></th>
<th class="head"><p>value</p></th>
<th class="head"><p>description</p></th>
</tr>
</thead>
<tbody>
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">BTF_VAR_STATIC</span></code></p></td>
<td><p>0x0</p></td>
<td><p>definition of global variable not visible outside containing compilation unit</p></td>
</tr>
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">BTF_VAR_GLOBAL_ALLOCATED</span></code></p></td>
<td><p>0x1</p></td>
<td><p>definition of global variable visible outside containing compilation unit</p></td>
</tr>
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">BTF_VAR_GLOBAL_EXTERN</span></code></p></td>
<td><p>0x2</p></td>
<td><p>declaration of global variable whose definition is outside the containing compilation unit</p></td>
</tr>
</tbody>
</table>
</section>
</section>
</section>
<section id="btf-kernel-api">
<h2>3. BTF Kernel API<a class="headerlink" href="#btf-kernel-api" title="Link to this heading">¶</a></h2>
<dl class="simple">
<dt>The following bpf syscall command involves BTF:</dt><dd><ul class="simple">
<li><p>BPF_BTF_LOAD: load a blob of BTF data into kernel</p></li>
<li><p>BPF_MAP_CREATE: map creation with btf key and value type info.</p></li>
<li><p>BPF_PROG_LOAD: prog load with btf function and line info.</p></li>
<li><p>BPF_BTF_GET_FD_BY_ID: get a btf fd</p></li>
<li><p>BPF_OBJ_GET_INFO_BY_FD: btf, func_info, line_info
and other btf related info are returned.</p></li>
</ul>
</dd>
</dl>
<p>The workflow typically looks like:</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>Application:
    BPF_BTF_LOAD
        |
        v
    BPF_MAP_CREATE and BPF_PROG_LOAD
        |
        V
    ......

Introspection tool:
    ......
    BPF_{PROG,MAP}_GET_NEXT_ID (get prog/map id&#39;s)
        |
        V
    BPF_{PROG,MAP}_GET_FD_BY_ID (get a prog/map fd)
        |
        V
    BPF_OBJ_GET_INFO_BY_FD (get bpf_prog_info/bpf_map_info with btf_id)
        |                                     |
        V                                     |
    BPF_BTF_GET_FD_BY_ID (get btf_fd)         |
        |                                     |
        V                                     |
    BPF_OBJ_GET_INFO_BY_FD (get btf)          |
        |                                     |
        V                                     V
    pretty print types, dump func signatures and line info, etc.
</pre></div>
</div>
<section id="bpf-btf-load">
<h3>3.1 BPF_BTF_LOAD<a class="headerlink" href="#bpf-btf-load" title="Link to this heading">¶</a></h3>
<p>Load a blob of BTF data into kernel. A blob of data, described in
<a class="reference internal" href="#btf-type-string"><span class="std std-ref">2. BTF Type and String Encoding</span></a>, can be directly loaded into the kernel. A <code class="docutils literal notranslate"><span class="pre">btf_fd</span></code>
is returned to a userspace.</p>
</section>
<section id="bpf-map-create">
<h3>3.2 BPF_MAP_CREATE<a class="headerlink" href="#bpf-map-create" title="Link to this heading">¶</a></h3>
<p>A map can be created with <code class="docutils literal notranslate"><span class="pre">btf_fd</span></code> and specified key/value type id.:</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>__u32   btf_fd;         /* fd pointing to a BTF type data */
__u32   btf_key_type_id;        /* BTF type_id of the key */
__u32   btf_value_type_id;      /* BTF type_id of the value */
</pre></div>
</div>
<p>In libbpf, the map can be defined with extra annotation like below:</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>struct {
    __uint(type, BPF_MAP_TYPE_ARRAY);
    __type(key, int);
    __type(value, struct ipv_counts);
    __uint(max_entries, 4);
} btf_map SEC(&quot;.maps&quot;);
</pre></div>
</div>
<p>During ELF parsing, libbpf is able to extract key/value type_id’s and assign
them to BPF_MAP_CREATE attributes automatically.</p>
</section>
<section id="bpf-prog-load">
<span id="id1"></span><h3>3.3 BPF_PROG_LOAD<a class="headerlink" href="#bpf-prog-load" title="Link to this heading">¶</a></h3>
<p>During prog_load, func_info and line_info can be passed to kernel with proper
values for the following attributes:</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>__u32           insn_cnt;
__aligned_u64   insns;
......
__u32           prog_btf_fd;    /* fd pointing to BTF type data */
__u32           func_info_rec_size;     /* userspace bpf_func_info size */
__aligned_u64   func_info;      /* func info */
__u32           func_info_cnt;  /* number of bpf_func_info records */
__u32           line_info_rec_size;     /* userspace bpf_line_info size */
__aligned_u64   line_info;      /* line info */
__u32           line_info_cnt;  /* number of bpf_line_info records */
</pre></div>
</div>
<p>The func_info and line_info are an array of below, respectively.:</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>struct bpf_func_info {
    __u32   insn_off; /* [0, insn_cnt - 1] */
    __u32   type_id;  /* pointing to a BTF_KIND_FUNC type */
};
struct bpf_line_info {
    __u32   insn_off; /* [0, insn_cnt - 1] */
    __u32   file_name_off; /* offset to string table for the filename */
    __u32   line_off; /* offset to string table for the source line */
    __u32   line_col; /* line number and column number */
};
</pre></div>
</div>
<p>func_info_rec_size is the size of each func_info record, and
line_info_rec_size is the size of each line_info record. Passing the record
size to kernel make it possible to extend the record itself in the future.</p>
<dl class="simple">
<dt>Below are requirements for func_info:</dt><dd><ul class="simple">
<li><p>func_info[0].insn_off must be 0.</p></li>
<li><p>the func_info insn_off is in strictly increasing order and matches
bpf func boundaries.</p></li>
</ul>
</dd>
<dt>Below are requirements for line_info:</dt><dd><ul class="simple">
<li><p>the first insn in each func must have a line_info record pointing to it.</p></li>
<li><p>the line_info insn_off is in strictly increasing order.</p></li>
</ul>
</dd>
</dl>
<p>For line_info, the line number and column number are defined as below:</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>#define BPF_LINE_INFO_LINE_NUM(line_col)        ((line_col) &gt;&gt; 10)
#define BPF_LINE_INFO_LINE_COL(line_col)        ((line_col) &amp; 0x3ff)
</pre></div>
</div>
</section>
<section id="bpf-prog-map-get-next-id">
<h3>3.4 BPF_{PROG,MAP}_GET_NEXT_ID<a class="headerlink" href="#bpf-prog-map-get-next-id" title="Link to this heading">¶</a></h3>
<p>In kernel, every loaded program, map or btf has a unique id. The id won’t
change during the lifetime of a program, map, or btf.</p>
<p>The bpf syscall command BPF_{PROG,MAP}_GET_NEXT_ID returns all id’s, one for
each command, to user space, for bpf program or maps, respectively, so an
inspection tool can inspect all programs and maps.</p>
</section>
<section id="bpf-prog-map-get-fd-by-id">
<h3>3.5 BPF_{PROG,MAP}_GET_FD_BY_ID<a class="headerlink" href="#bpf-prog-map-get-fd-by-id" title="Link to this heading">¶</a></h3>
<p>An introspection tool cannot use id to get details about program or maps.
A file descriptor needs to be obtained first for reference-counting purpose.</p>
</section>
<section id="bpf-obj-get-info-by-fd">
<h3>3.6 BPF_OBJ_GET_INFO_BY_FD<a class="headerlink" href="#bpf-obj-get-info-by-fd" title="Link to this heading">¶</a></h3>
<p>Once a program/map fd is acquired, an introspection tool can get the detailed
information from kernel about this fd, some of which are BTF-related. For
example, <code class="docutils literal notranslate"><span class="pre">bpf_map_info</span></code> returns <code class="docutils literal notranslate"><span class="pre">btf_id</span></code> and key/value type ids.
<code class="docutils literal notranslate"><span class="pre">bpf_prog_info</span></code> returns <code class="docutils literal notranslate"><span class="pre">btf_id</span></code>, func_info, and line info for translated
bpf byte codes, and jited_line_info.</p>
</section>
<section id="bpf-btf-get-fd-by-id">
<h3>3.7 BPF_BTF_GET_FD_BY_ID<a class="headerlink" href="#bpf-btf-get-fd-by-id" title="Link to this heading">¶</a></h3>
<p>With <code class="docutils literal notranslate"><span class="pre">btf_id</span></code> obtained in <code class="docutils literal notranslate"><span class="pre">bpf_map_info</span></code> and <code class="docutils literal notranslate"><span class="pre">bpf_prog_info</span></code>, bpf
syscall command BPF_BTF_GET_FD_BY_ID can retrieve a btf fd. Then, with
command BPF_OBJ_GET_INFO_BY_FD, the btf blob, originally loaded into the
kernel with BPF_BTF_LOAD, can be retrieved.</p>
<p>With the btf blob, <code class="docutils literal notranslate"><span class="pre">bpf_map_info</span></code>, and <code class="docutils literal notranslate"><span class="pre">bpf_prog_info</span></code>, an introspection
tool has full btf knowledge and is able to pretty print map key/values, dump
func signatures and line info, along with byte/jit codes.</p>
</section>
</section>
<section id="elf-file-format-interface">
<h2>4. ELF File Format Interface<a class="headerlink" href="#elf-file-format-interface" title="Link to this heading">¶</a></h2>
<section id="btf-section">
<h3>4.1 .BTF section<a class="headerlink" href="#btf-section" title="Link to this heading">¶</a></h3>
<p>The .BTF section contains type and string data. The format of this section is
same as the one describe in <a class="reference internal" href="#btf-type-string"><span class="std std-ref">2. BTF Type and String Encoding</span></a>.</p>
</section>
<section id="btf-ext-section">
<span id="id2"></span><h3>4.2 .BTF.ext section<a class="headerlink" href="#btf-ext-section" title="Link to this heading">¶</a></h3>
<p>The .BTF.ext section encodes func_info, line_info and CO-RE relocations
which needs loader manipulation before loading into the kernel.</p>
<p>The specification for .BTF.ext section is defined at <code class="docutils literal notranslate"><span class="pre">tools/lib/bpf/btf.h</span></code>
and <code class="docutils literal notranslate"><span class="pre">tools/lib/bpf/btf.c</span></code>.</p>
<p>The current header of .BTF.ext section:</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>struct btf_ext_header {
    __u16   magic;
    __u8    version;
    __u8    flags;
    __u32   hdr_len;

    /* All offsets are in bytes relative to the end of this header */
    __u32   func_info_off;
    __u32   func_info_len;
    __u32   line_info_off;
    __u32   line_info_len;

    /* optional part of .BTF.ext header */
    __u32   core_relo_off;
    __u32   core_relo_len;
};
</pre></div>
</div>
<p>It is very similar to .BTF section. Instead of type/string section, it
contains func_info, line_info and core_relo sub-sections.
See <a class="reference internal" href="#bpf-prog-load"><span class="std std-ref">3.3 BPF_PROG_LOAD</span></a> for details about func_info and line_info
record format.</p>
<p>The func_info is organized as below.:</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>func_info_rec_size              /* __u32 value */
btf_ext_info_sec for section #1 /* func_info for section #1 */
btf_ext_info_sec for section #2 /* func_info for section #2 */
...
</pre></div>
</div>
<p><code class="docutils literal notranslate"><span class="pre">func_info_rec_size</span></code> specifies the size of <code class="docutils literal notranslate"><span class="pre">bpf_func_info</span></code> structure when
.BTF.ext is generated. <code class="docutils literal notranslate"><span class="pre">btf_ext_info_sec</span></code>, defined below, is a collection of
func_info for each specific ELF section.:</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>struct btf_ext_info_sec {
   __u32   sec_name_off; /* offset to section name */
   __u32   num_info;
   /* Followed by num_info * record_size number of bytes */
   __u8    data[0];
};
</pre></div>
</div>
<p>Here, num_info must be greater than 0.</p>
<p>The line_info is organized as below.:</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>line_info_rec_size              /* __u32 value */
btf_ext_info_sec for section #1 /* line_info for section #1 */
btf_ext_info_sec for section #2 /* line_info for section #2 */
...
</pre></div>
</div>
<p><code class="docutils literal notranslate"><span class="pre">line_info_rec_size</span></code> specifies the size of <code class="docutils literal notranslate"><span class="pre">bpf_line_info</span></code> structure when
.BTF.ext is generated.</p>
<p>The interpretation of <code class="docutils literal notranslate"><span class="pre">bpf_func_info-&gt;insn_off</span></code> and
<code class="docutils literal notranslate"><span class="pre">bpf_line_info-&gt;insn_off</span></code> is different between kernel API and ELF API. For
kernel API, the <code class="docutils literal notranslate"><span class="pre">insn_off</span></code> is the instruction offset in the unit of <code class="docutils literal notranslate"><span class="pre">struct</span>
<span class="pre">bpf_insn</span></code>. For ELF API, the <code class="docutils literal notranslate"><span class="pre">insn_off</span></code> is the byte offset from the
beginning of section (<code class="docutils literal notranslate"><span class="pre">btf_ext_info_sec-&gt;sec_name_off</span></code>).</p>
<p>The core_relo is organized as below.:</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>core_relo_rec_size              /* __u32 value */
btf_ext_info_sec for section #1 /* core_relo for section #1 */
btf_ext_info_sec for section #2 /* core_relo for section #2 */
</pre></div>
</div>
<p><code class="docutils literal notranslate"><span class="pre">core_relo_rec_size</span></code> specifies the size of <code class="docutils literal notranslate"><span class="pre">bpf_core_relo</span></code>
structure when .BTF.ext is generated. All <code class="docutils literal notranslate"><span class="pre">bpf_core_relo</span></code> structures
within a single <code class="docutils literal notranslate"><span class="pre">btf_ext_info_sec</span></code> describe relocations applied to
section named by <code class="docutils literal notranslate"><span class="pre">btf_ext_info_sec-&gt;sec_name_off</span></code>.</p>
<p>See <a class="reference internal" href="llvm_reloc.html#btf-co-re-relocations"><span class="std std-ref">Documentation/bpf/llvm_reloc.rst</span></a>
for more information on CO-RE relocations.</p>
</section>
<section id="btf-ids-section">
<h3>4.3 .BTF_ids section<a class="headerlink" href="#btf-ids-section" title="Link to this heading">¶</a></h3>
<p>The .BTF_ids section encodes BTF ID values that are used within the kernel.</p>
<p>This section is created during the kernel compilation with the help of
macros defined in <code class="docutils literal notranslate"><span class="pre">include/linux/btf_ids.h</span></code> header file. Kernel code can
use them to create lists and sets (sorted lists) of BTF ID values.</p>
<p>The <code class="docutils literal notranslate"><span class="pre">BTF_ID_LIST</span></code> and <code class="docutils literal notranslate"><span class="pre">BTF_ID</span></code> macros define unsorted list of BTF ID values,
with following syntax:</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>BTF_ID_LIST(list)
BTF_ID(type1, name1)
BTF_ID(type2, name2)
</pre></div>
</div>
<p>resulting in following layout in .BTF_ids section:</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>__BTF_ID__type1__name1__1:
.zero 4
__BTF_ID__type2__name2__2:
.zero 4
</pre></div>
</div>
<p>The <code class="docutils literal notranslate"><span class="pre">u32</span> <span class="pre">list[];</span></code> variable is defined to access the list.</p>
<p>The <code class="docutils literal notranslate"><span class="pre">BTF_ID_UNUSED</span></code> macro defines 4 zero bytes. It’s used when we
want to define unused entry in BTF_ID_LIST, like:</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>BTF_ID_LIST(bpf_skb_output_btf_ids)
BTF_ID(struct, sk_buff)
BTF_ID_UNUSED
BTF_ID(struct, task_struct)
</pre></div>
</div>
<p>The <code class="docutils literal notranslate"><span class="pre">BTF_SET_START/END</span></code> macros pair defines sorted list of BTF ID values
and their count, with following syntax:</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>BTF_SET_START(set)
BTF_ID(type1, name1)
BTF_ID(type2, name2)
BTF_SET_END(set)
</pre></div>
</div>
<p>resulting in following layout in .BTF_ids section:</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>__BTF_ID__set__set:
.zero 4
__BTF_ID__type1__name1__3:
.zero 4
__BTF_ID__type2__name2__4:
.zero 4
</pre></div>
</div>
<p>The <code class="docutils literal notranslate"><span class="pre">struct</span> <span class="pre">btf_id_set</span> <span class="pre">set;</span></code> variable is defined to access the list.</p>
<p>The <code class="docutils literal notranslate"><span class="pre">typeX</span></code> name can be one of following:</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>struct, union, typedef, func
</pre></div>
</div>
<p>and is used as a filter when resolving the BTF ID value.</p>
<p>All the BTF ID lists and sets are compiled in the .BTF_ids section and
resolved during the linking phase of kernel build by <code class="docutils literal notranslate"><span class="pre">resolve_btfids</span></code> tool.</p>
</section>
<section id="btf-base-section">
<h3>4.4 .BTF.base section<a class="headerlink" href="#btf-base-section" title="Link to this heading">¶</a></h3>
<p>Split BTF - where the .BTF section only contains types not in the associated
base .BTF section - is an extremely efficient way to encode type information
for kernel modules, since they generally consist of a few module-specific
types along with a large set of shared kernel types. The former are encoded
in split BTF, while the latter are encoded in base BTF, resulting in more
compact representations. A type in split BTF that refers to a type in
base BTF refers to it using its base BTF ID, and split BTF IDs start
at last_base_BTF_ID + 1.</p>
<p>The downside of this approach however is that this makes the split BTF
somewhat brittle - when the base BTF changes, base BTF ID references are
no longer valid and the split BTF itself becomes useless. The role of the
.BTF.base section is to make split BTF more resilient for cases where
the base BTF may change, as is the case for kernel modules not built every
time the kernel is for example. .BTF.base contains named base types; INTs,
FLOATs, STRUCTs, UNIONs, ENUM[64]s and FWDs. INTs and FLOATs are fully
described in .BTF.base sections, while composite types like structs
and unions are not fully defined - the .BTF.base type simply serves as
a description of the type the split BTF referred to, so structs/unions
have 0 members in the .BTF.base section. ENUM[64]s are similarly recorded
with 0 members. Any other types are added to the split BTF. This
distillation process then leaves us with a .BTF.base section with
such minimal descriptions of base types and .BTF split section which refers
to those base types. Later, we can relocate the split BTF using both the
information stored in the .BTF.base section and the new .BTF base; the type
information in the .BTF.base section allows us to update the split BTF
references to point at the corresponding new base BTF IDs.</p>
<p>BTF relocation happens on kernel module load when a kernel module has a
.BTF.base section, and libbpf also provides a <code class="xref c c-func broken_xref docutils literal notranslate"><span class="pre">btf__relocate()</span></code> API to
accomplish this.</p>
<p>As an example consider the following base BTF:</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>[1] INT &#39;int&#39; size=4 bits_offset=0 nr_bits=32 encoding=SIGNED
[2] STRUCT &#39;foo&#39; size=8 vlen=2
        &#39;f1&#39; type_id=1 bits_offset=0
        &#39;f2&#39; type_id=1 bits_offset=32
</pre></div>
</div>
<p>...and associated split BTF:</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>[3] PTR &#39;(anon)&#39; type_id=2
</pre></div>
</div>
<p>i.e. split BTF describes a pointer to <code class="xref c c-struct broken_xref docutils literal notranslate"><span class="pre">struct</span> <span class="pre">foo</span></code> { int f1; int f2 };</p>
<p>.BTF.base will consist of:</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>[1] INT &#39;int&#39; size=4 bits_offset=0 nr_bits=32 encoding=SIGNED
[2] STRUCT &#39;foo&#39; size=8 vlen=0
</pre></div>
</div>
<p>If we relocate the split BTF later using the following new base BTF:</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>[1] INT &#39;long unsigned int&#39; size=8 bits_offset=0 nr_bits=64 encoding=(none)
[2] INT &#39;int&#39; size=4 bits_offset=0 nr_bits=32 encoding=SIGNED
[3] STRUCT &#39;foo&#39; size=8 vlen=2
        &#39;f1&#39; type_id=2 bits_offset=0
        &#39;f2&#39; type_id=2 bits_offset=32
</pre></div>
</div>
<p>...we can use our .BTF.base description to know that the split BTF reference
is to <code class="xref c c-struct broken_xref docutils literal notranslate"><span class="pre">struct</span> <span class="pre">foo</span></code>, and relocation results in new split BTF:</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>[4] PTR &#39;(anon)&#39; type_id=3
</pre></div>
</div>
<p>Note that we had to update BTF ID and start BTF ID for the split BTF.</p>
<p>So we see how .BTF.base plays the role of facilitating later relocation,
leading to more resilient split BTF.</p>
<p>.BTF.base sections will be generated automatically for out-of-tree kernel module
builds - i.e. where KBUILD_EXTMOD is set (as it would be for “make M=path/2/mod”
cases). .BTF.base generation requires pahole support for the “distilled_base”
BTF feature; this is available in pahole v1.28 and later.</p>
</section>
</section>
<section id="using-btf">
<h2>5. Using BTF<a class="headerlink" href="#using-btf" title="Link to this heading">¶</a></h2>
<section id="bpftool-map-pretty-print">
<h3>5.1 bpftool map pretty print<a class="headerlink" href="#bpftool-map-pretty-print" title="Link to this heading">¶</a></h3>
<p>With BTF, the map key/value can be printed based on fields rather than simply
raw bytes. This is especially valuable for large structure or if your data
structure has bitfields. For example, for the following map,:</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>enum A { A1, A2, A3, A4, A5 };
typedef enum A ___A;
struct tmp_t {
     char a1:4;
     int  a2:4;
     int  :4;
     __u32 a3:4;
     int b;
     ___A b1:4;
     enum A b2:4;
};
struct {
     __uint(type, BPF_MAP_TYPE_ARRAY);
     __type(key, int);
     __type(value, struct tmp_t);
     __uint(max_entries, 1);
} tmpmap SEC(&quot;.maps&quot;);
</pre></div>
</div>
<p>bpftool is able to pretty print like below:</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>[{
      &quot;key&quot;: 0,
      &quot;value&quot;: {
          &quot;a1&quot;: 0x2,
          &quot;a2&quot;: 0x4,
          &quot;a3&quot;: 0x6,
          &quot;b&quot;: 7,
          &quot;b1&quot;: 0x8,
          &quot;b2&quot;: 0xa
      }
  }
]
</pre></div>
</div>
</section>
<section id="bpftool-prog-dump">
<h3>5.2 bpftool prog dump<a class="headerlink" href="#bpftool-prog-dump" title="Link to this heading">¶</a></h3>
<p>The following is an example showing how func_info and line_info can help prog
dump with better kernel symbol names, function prototypes and line
information.:</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>$ bpftool prog dump jited pinned /sys/fs/bpf/test_btf_haskv
[...]
int test_long_fname_2(struct dummy_tracepoint_args * arg):
bpf_prog_44a040bf25481309_test_long_fname_2:
; static int test_long_fname_2(struct dummy_tracepoint_args *arg)
   0:   push   %rbp
   1:   mov    %rsp,%rbp
   4:   sub    $0x30,%rsp
   b:   sub    $0x28,%rbp
   f:   mov    %rbx,0x0(%rbp)
  13:   mov    %r13,0x8(%rbp)
  17:   mov    %r14,0x10(%rbp)
  1b:   mov    %r15,0x18(%rbp)
  1f:   xor    %eax,%eax
  21:   mov    %rax,0x20(%rbp)
  25:   xor    %esi,%esi
; int key = 0;
  27:   mov    %esi,-0x4(%rbp)
; if (!arg-&gt;sock)
  2a:   mov    0x8(%rdi),%rdi
; if (!arg-&gt;sock)
  2e:   cmp    $0x0,%rdi
  32:   je     0x0000000000000070
  34:   mov    %rbp,%rsi
; counts = bpf_map_lookup_elem(&amp;btf_map, &amp;key);
[...]
</pre></div>
</div>
</section>
<section id="verifier-log">
<h3>5.3 Verifier Log<a class="headerlink" href="#verifier-log" title="Link to this heading">¶</a></h3>
<p>The following is an example of how line_info can help debugging verification
failure.:</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>   /* The code at tools/testing/selftests/bpf/test_xdp_noinline.c
    * is modified as below.
    */
   data = (void *)(long)xdp-&gt;data;
   data_end = (void *)(long)xdp-&gt;data_end;
   /*
   if (data + 4 &gt; data_end)
           return XDP_DROP;
   */
   *(u32 *)data = dst-&gt;dst;

$ bpftool prog load ./test_xdp_noinline.o /sys/fs/bpf/test_xdp_noinline type xdp
    ; data = (void *)(long)xdp-&gt;data;
    224: (79) r2 = *(u64 *)(r10 -112)
    225: (61) r2 = *(u32 *)(r2 +0)
    ; *(u32 *)data = dst-&gt;dst;
    226: (63) *(u32 *)(r2 +0) = r1
    invalid access to packet, off=0 size=4, R2(id=0,off=0,r=0)
    R2 offset is outside of the packet
</pre></div>
</div>
</section>
</section>
<section id="btf-generation">
<h2>6. BTF Generation<a class="headerlink" href="#btf-generation" title="Link to this heading">¶</a></h2>
<p>You need latest pahole</p>
<blockquote>
<div><p><a class="reference external" href="https://git.kernel.org/pub/scm/devel/pahole/pahole.git/">https://git.kernel.org/pub/scm/devel/pahole/pahole.git/</a></p>
</div></blockquote>
<p>or llvm (8.0 or later). The pahole acts as a dwarf2btf converter. It doesn’t
support .BTF.ext and btf BTF_KIND_FUNC type yet. For example,:</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>-bash-4.4$ cat t.c
struct t {
  int a:2;
  int b:3;
  int c:2;
} g;
-bash-4.4$ gcc -c -O2 -g t.c
-bash-4.4$ pahole -JV t.o
File t.o:
[1] STRUCT t kind_flag=1 size=4 vlen=3
        a type_id=2 bitfield_size=2 bits_offset=0
        b type_id=2 bitfield_size=3 bits_offset=2
        c type_id=2 bitfield_size=2 bits_offset=5
[2] INT int size=4 bit_offset=0 nr_bits=32 encoding=SIGNED
</pre></div>
</div>
<p>The llvm is able to generate .BTF and .BTF.ext directly with -g for bpf target
only. The assembly code (-S) is able to show the BTF encoding in assembly
format.:</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>-bash-4.4$ cat t2.c
typedef int __int32;
struct t2 {
  int a2;
  int (*f2)(char q1, __int32 q2, ...);
  int (*f3)();
} g2;
int main() { return 0; }
int test() { return 0; }
-bash-4.4$ clang -c -g -O2 --target=bpf t2.c
-bash-4.4$ readelf -S t2.o
  ......
  [ 8] .BTF              PROGBITS         0000000000000000  00000247
       000000000000016e  0000000000000000           0     0     1
  [ 9] .BTF.ext          PROGBITS         0000000000000000  000003b5
       0000000000000060  0000000000000000           0     0     1
  [10] .rel.BTF.ext      REL              0000000000000000  000007e0
       0000000000000040  0000000000000010          16     9     8
  ......
-bash-4.4$ clang -S -g -O2 --target=bpf t2.c
-bash-4.4$ cat t2.s
  ......
        .section        .BTF,&quot;&quot;,@progbits
        .short  60319                   # 0xeb9f
        .byte   1
        .byte   0
        .long   24
        .long   0
        .long   220
        .long   220
        .long   122
        .long   0                       # BTF_KIND_FUNC_PROTO(id = 1)
        .long   218103808               # 0xd000000
        .long   2
        .long   83                      # BTF_KIND_INT(id = 2)
        .long   16777216                # 0x1000000
        .long   4
        .long   16777248                # 0x1000020
  ......
        .byte   0                       # string offset=0
        .ascii  &quot;.text&quot;                 # string offset=1
        .byte   0
        .ascii  &quot;/home/yhs/tmp-pahole/t2.c&quot; # string offset=7
        .byte   0
        .ascii  &quot;int main() { return 0; }&quot; # string offset=33
        .byte   0
        .ascii  &quot;int test() { return 0; }&quot; # string offset=58
        .byte   0
        .ascii  &quot;int&quot;                   # string offset=83
  ......
        .section        .BTF.ext,&quot;&quot;,@progbits
        .short  60319                   # 0xeb9f
        .byte   1
        .byte   0
        .long   24
        .long   0
        .long   28
        .long   28
        .long   44
        .long   8                       # FuncInfo
        .long   1                       # FuncInfo section string offset=1
        .long   2
        .long   .Lfunc_begin0
        .long   3
        .long   .Lfunc_begin1
        .long   5
        .long   16                      # LineInfo
        .long   1                       # LineInfo section string offset=1
        .long   2
        .long   .Ltmp0
        .long   7
        .long   33
        .long   7182                    # Line 7 Col 14
        .long   .Ltmp3
        .long   7
        .long   58
        .long   8206                    # Line 8 Col 14
</pre></div>
</div>
</section>
<section id="testing">
<h2>7. Testing<a class="headerlink" href="#testing" title="Link to this heading">¶</a></h2>
<p>The kernel BPF selftest <a class="reference external" href="https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/tree/tools/testing/selftests/bpf/prog_tests/btf.c">tools/testing/selftests/bpf/prog_tests/btf.c</a>
provides an extensive set of BTF-related tests.</p>
</section>
</section>


          </div>
          
        </div>
      </div>
    <div class="clearer"></div>
  </div>
    <div class="footer">
      &#169;The kernel development community.
      
      |
      Powered by <a href="https://www.sphinx-doc.org/">Sphinx 8.1.3</a>
      &amp; <a href="https://alabaster.readthedocs.io">Alabaster 0.7.16</a>
      
      |
      <a href="../_sources/bpf/btf.rst.txt"
          rel="nofollow">Page source</a>
    </div>

    

    
  </body>
</html>

Filemanager

Name Type Size Permission Actions
libbpf Folder 0755
standardization Folder 0755
bpf_design_QA.html File 39.76 KB 0644
bpf_devel_QA.html File 61.1 KB 0644
bpf_iterators.html File 43.95 KB 0644
bpf_licensing.html File 14.62 KB 0644
bpf_prog_run.html File 19 KB 0644
btf.html File 89.32 KB 0644
clang-notes.html File 13.55 KB 0644
classic_vs_extended.html File 26.83 KB 0644
cpumasks.html File 97.17 KB 0644
drgn.html File 20.27 KB 0644
faq.html File 10.01 KB 0644
fs_kfuncs.html File 10.67 KB 0644
graph_ds_impl.html File 30.56 KB 0644
helpers.html File 9.82 KB 0644
index.html File 12.27 KB 0644
kfuncs.html File 67.22 KB 0644
linux-notes.html File 16.73 KB 0644
llvm_reloc.html File 77.97 KB 0644
map_array.html File 37.73 KB 0644
map_bloom_filter.html File 26.42 KB 0644
map_cgroup_storage.html File 18.5 KB 0644
map_cgrp_storage.html File 16.3 KB 0644
map_cpumap.html File 29.43 KB 0644
map_devmap.html File 35.99 KB 0644
map_hash.html File 35.25 KB 0644
map_lpm_trie.html File 28.12 KB 0644
map_of_maps.html File 23.2 KB 0644
map_queue_stack.html File 23.69 KB 0644
map_sk_storage.html File 26.15 KB 0644
map_sockmap.html File 64.85 KB 0644
map_xskmap.html File 26.94 KB 0644
maps.html File 19.06 KB 0644
other.html File 10.06 KB 0644
prog_cgroup_sockopt.html File 27.23 KB 0644
prog_cgroup_sysctl.html File 18.75 KB 0644
prog_flow_dissector.html File 20.62 KB 0644
prog_lsm.html File 22.69 KB 0644
prog_sk_lookup.html File 16.62 KB 0644
programs.html File 10.56 KB 0644
redirect.html File 15.24 KB 0644
ringbuf.html File 24.44 KB 0644
s390.html File 17.98 KB 0644
syscall_api.html File 10.03 KB 0644
test_debug.html File 9.89 KB 0644
verifier.html File 37.16 KB 0644
Filemanager