hugo/content/posts/libbpf_minimal.md
2022-03-07 09:27:34 +08:00

2.4 KiB
Raw Blame History

+++ title = "Libbpf Minimal分析" author = ["Logic"] date = 2022-03-06 draft = false +++

宏定义分析

Minimal程序使用了SEC宏展开后结果分析如下

SEC

展开后为

#define SEC(name) \
	_Pragma("GCC diagnostic push")					    \
	_Pragma("GCC diagnostic ignored \"-Wignored-attributes\"")	    \
	__attribute__((section(name), used))				    \
	_Pragma("GCC diagnostic pop")					    \

源文件中对应给出的注释为

/*

  • Helper macro to place programs, maps, license in
  • different sections in elf_bpf file. Section names
  • are interpreted by libbpf depending on the context (BPF programs, BPF maps,
  • extern variables, etc).
  • To allow use of SEC() with externs (e.g., for extern .maps declarations),
  • make sure attribute((unused)) doesn't trigger compilation warning.

*/

从注释来看该宏的作用在于将bpf程序的不同部分放入最终生成的elf文件的不同节中。这个宏是怎么起到这样的作用的。对宏展开后的内容分析。

_Pragma

其中有三句为_Pragma,这是一个C99新引入的运算符作为运算符的优势在于其可以用于宏中如该源文件中的SEC宏。该运算符在处理运算符内的表达式后即相当于#pragma指令。处理过程非常简单将'\\'替换为'\',将\"替换为"。这样_Pragma在预处理后即成为~ #pragma GCC diagnostic ignored "-Wignored-attributes" ~。pragma则是针对编译器给出的控制编译的指令因此相关信息需要查阅gcc的说明。在该网站可以看到 gcc支持的pragma 。其中该处使用的pragma为 Diagnostic Pragmas ,这里要注意的是这个编译指令会覆盖编译器的命令行参数push即改变状态忽视Wignored-attributes参数pop则恢复原状。

__attribute__

__attribute__也是一种编译器指令用于在声明函数变量类型时帮助声明对象向编译器告知某些编译特性。在gnu网站上可以查知 可用的属性 ,在此处的含义为将对应的函数放入指定的name段中并且即使该函数没有被引用也不允许将该自定义的段优化掉used