title: eBPF: global variables and structs
body: <p>So I have a simple <code>eBPF</code> code:</p>
  
  <p>my.h:</p>
  
  <pre><code>#ifndef __MY_COMMON_H__
  #define __MY_COMMON_H__
  
  #include &lt;linux/types.h&gt;
  
  struct foo {
          int a;
          int b;
          int c;
          int d;
  };
  
  #endif  /* __MY_COMMON_H__ */
  </code></pre>
  
  <p>my_kern.c:</p>
  
  <pre><code>...
  struct bpf_map_def SEC("maps") my_map = {
          .type = BPF_MAP_TYPE_HASH,
          .key_size = ...,
          .value_size = ...,
          .max_entries = MAX_ENTRIES,
  };
  
  struct foo my_foo = {
          .a = 150000,
          .b = 100,
          .c = 10,
          .d = 40,
  };
  
  SEC("sockops")
  int my_bpf(struct bpf_sock_ops *sk_ops)
  {
     ...
  
  };
  
  char _license[] SEC("license") = "GPL";
  u32 _version SEC("version") = LINUX_VERSION_CODE;
  </code></pre>
  
  <p>I build the code with <code>llvm-5.0</code>, with no errors/warnings, however <code>bpftool prog load ...</code> fails:</p>
  
  <pre><code>libbpf: Program 'sockops' contains non-map related relo data pointing to section 6
  Error: failed to load program
  
  
  $ llvm-readelf-5.0 -s my_kern.o
  There are 12 section headers, starting at offset 0xa90:
  
  Section Headers:
    [Nr] Name              Type            Address          Off    Size   ES Flg Lk Inf Al
    [ 0]                   NULL            0000000000000000 000000 000000 00      0   0  0
    [ 1] .strtab           STRTAB          0000000000000000 0009c0 0000cc 00      0   0  1
    [ 2] .text             PROGBITS        0000000000000000 000040 000000 00  AX  0   0  4
    [ 3] sockops           PROGBITS        0000000000000000 000040 0006e0 00  AX  0   0  8
    [ 4] .relsockops       REL             0000000000000000 000980 000040 10     11   3  8
    [ 5] maps              PROGBITS        0000000000000000 000720 00001c 00  WA  0   0  4
    [ 6] .data             PROGBITS        0000000000000000 00073c 00001c 00  WA  0   0  4
    [ 7] .rodata.str1.16   PROGBITS        0000000000000000 000760 000093 01 AMS  0   0 16
    [ 8] .rodata.str1.1    PROGBITS        0000000000000000 0007f3 00001d 01 AMS  0   0  1
    [ 9] license           PROGBITS        0000000000000000 000810 000004 00  WA  0   0  1
    [10] version           PROGBITS        0000000000000000 000814 000004 00  WA  0   0  4
    [11] .symtab           SYMTAB          0000000000000000 000818 000168 18      1  10  8
  Key to Flags:
    W (write), A (alloc), X (execute), M (merge), S (strings), l (large)
    I (info), L (link order), G (group), T (TLS), E (exclude), x (unknown)
    O (extra OS processing required) o (OS specific), p (processor specific)
  $
  </code></pre>
  
  <p>Section 6 contains my <code>my_foo</code> structure, I could dump its contents with <code>llvm-objdump</code>.</p>
  
  <p>This error does not happen if I define <code>my_foo</code> inside <code>main()</code> function for instance. Does it mean such global declarations are not permitted by eBPF convention?</p>