obj.rs 94 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801
  1. //! Object file loading, parsing, and relocation.
  2. use alloc::{
  3. borrow::ToOwned,
  4. collections::BTreeMap,
  5. ffi::CString,
  6. string::{String, ToString},
  7. vec,
  8. vec::Vec,
  9. };
  10. use core::{ffi::CStr, mem, ptr, slice::from_raw_parts_mut, str::FromStr};
  11. use log::debug;
  12. use object::{
  13. read::{Object as ElfObject, ObjectSection, Section as ObjSection},
  14. Endianness, ObjectSymbol, ObjectSymbolTable, RelocationTarget, SectionIndex, SectionKind,
  15. SymbolKind,
  16. };
  17. use crate::{
  18. btf::{
  19. Array, Btf, BtfError, BtfExt, BtfFeatures, BtfType, DataSecEntry, FuncSecInfo, LineSecInfo,
  20. },
  21. generated::{
  22. bpf_func_id::*, bpf_insn, bpf_map_info, bpf_map_type::BPF_MAP_TYPE_ARRAY, BPF_CALL,
  23. BPF_F_RDONLY_PROG, BPF_JMP, BPF_K,
  24. },
  25. maps::{bpf_map_def, BtfMap, BtfMapDef, LegacyMap, Map, PinningType, MINIMUM_MAP_SIZE},
  26. programs::{
  27. CgroupSockAddrAttachType, CgroupSockAttachType, CgroupSockoptAttachType, XdpAttachType,
  28. },
  29. relocation::*,
  30. util::HashMap,
  31. };
  32. const KERNEL_VERSION_ANY: u32 = 0xFFFF_FFFE;
  33. /// Features implements BPF and BTF feature detection
  34. #[derive(Default, Debug)]
  35. #[allow(missing_docs)]
  36. pub struct Features {
  37. bpf_name: bool,
  38. bpf_probe_read_kernel: bool,
  39. bpf_perf_link: bool,
  40. bpf_global_data: bool,
  41. bpf_cookie: bool,
  42. cpumap_prog_id: bool,
  43. devmap_prog_id: bool,
  44. prog_info_map_ids: bool,
  45. prog_info_gpl_compatible: bool,
  46. btf: Option<BtfFeatures>,
  47. }
  48. impl Features {
  49. #[doc(hidden)]
  50. #[allow(clippy::too_many_arguments)]
  51. pub fn new(
  52. bpf_name: bool,
  53. bpf_probe_read_kernel: bool,
  54. bpf_perf_link: bool,
  55. bpf_global_data: bool,
  56. bpf_cookie: bool,
  57. cpumap_prog_id: bool,
  58. devmap_prog_id: bool,
  59. prog_info_map_ids: bool,
  60. prog_info_gpl_compatible: bool,
  61. btf: Option<BtfFeatures>,
  62. ) -> Self {
  63. Self {
  64. bpf_name,
  65. bpf_probe_read_kernel,
  66. bpf_perf_link,
  67. bpf_global_data,
  68. bpf_cookie,
  69. cpumap_prog_id,
  70. devmap_prog_id,
  71. prog_info_map_ids,
  72. prog_info_gpl_compatible,
  73. btf,
  74. }
  75. }
  76. /// Returns whether BPF program names and map names are supported.
  77. ///
  78. /// Although the feature probe performs the check for program name, we can use this to also
  79. /// detect if map name is supported since they were both introduced in the same commit.
  80. pub fn bpf_name(&self) -> bool {
  81. self.bpf_name
  82. }
  83. /// Returns whether the bpf_probe_read_kernel helper is supported.
  84. pub fn bpf_probe_read_kernel(&self) -> bool {
  85. self.bpf_probe_read_kernel
  86. }
  87. /// Returns whether bpf_links are supported for Kprobes/Uprobes/Tracepoints.
  88. pub fn bpf_perf_link(&self) -> bool {
  89. self.bpf_perf_link
  90. }
  91. /// Returns whether BPF program global data is supported.
  92. pub fn bpf_global_data(&self) -> bool {
  93. self.bpf_global_data
  94. }
  95. /// Returns whether BPF program cookie is supported.
  96. pub fn bpf_cookie(&self) -> bool {
  97. self.bpf_cookie
  98. }
  99. /// Returns whether XDP CPU Maps support chained program IDs.
  100. pub fn cpumap_prog_id(&self) -> bool {
  101. self.cpumap_prog_id
  102. }
  103. /// Returns whether XDP Device Maps support chained program IDs.
  104. pub fn devmap_prog_id(&self) -> bool {
  105. self.devmap_prog_id
  106. }
  107. /// Returns whether `bpf_prog_info` supports `nr_map_ids` & `map_ids` fields.
  108. pub fn prog_info_map_ids(&self) -> bool {
  109. self.prog_info_map_ids
  110. }
  111. /// Returns whether `bpf_prog_info` supports `gpl_compatible` field.
  112. pub fn prog_info_gpl_compatible(&self) -> bool {
  113. self.prog_info_gpl_compatible
  114. }
  115. /// If BTF is supported, returns which BTF features are supported.
  116. pub fn btf(&self) -> Option<&BtfFeatures> {
  117. self.btf.as_ref()
  118. }
  119. }
  120. /// The loaded object file representation
  121. #[derive(Clone, Debug)]
  122. pub struct Object {
  123. /// The endianness
  124. pub endianness: Endianness,
  125. /// Program license
  126. pub license: CString,
  127. /// Kernel version
  128. pub kernel_version: Option<u32>,
  129. /// Program BTF
  130. pub btf: Option<Btf>,
  131. /// Program BTF.ext
  132. pub btf_ext: Option<BtfExt>,
  133. /// Referenced maps
  134. pub maps: HashMap<String, Map>,
  135. /// A hash map of programs, using the program names parsed
  136. /// in [ProgramSection]s as keys.
  137. pub programs: HashMap<String, Program>,
  138. /// Functions
  139. pub functions: BTreeMap<(usize, u64), Function>,
  140. pub(crate) relocations: HashMap<SectionIndex, HashMap<u64, Relocation>>,
  141. pub(crate) symbol_table: HashMap<usize, Symbol>,
  142. pub(crate) symbols_by_section: HashMap<SectionIndex, Vec<usize>>,
  143. pub(crate) section_infos: HashMap<String, (SectionIndex, u64)>,
  144. // symbol_offset_by_name caches symbols that could be referenced from a
  145. // BTF VAR type so the offsets can be fixed up
  146. pub(crate) symbol_offset_by_name: HashMap<String, u64>,
  147. }
  148. /// An eBPF program
  149. #[derive(Debug, Clone)]
  150. pub struct Program {
  151. /// The license
  152. pub license: CString,
  153. /// The kernel version
  154. pub kernel_version: Option<u32>,
  155. /// The section containing the program
  156. pub section: ProgramSection,
  157. /// The section index of the program
  158. pub section_index: usize,
  159. /// The address of the program
  160. pub address: u64,
  161. }
  162. impl Program {
  163. /// The key used by [Object::functions]
  164. pub fn function_key(&self) -> (usize, u64) {
  165. (self.section_index, self.address)
  166. }
  167. }
  168. /// An eBPF function
  169. #[derive(Debug, Clone)]
  170. pub struct Function {
  171. /// The address
  172. pub address: u64,
  173. /// The function name
  174. pub name: String,
  175. /// The section index
  176. pub section_index: SectionIndex,
  177. /// The section offset
  178. pub section_offset: usize,
  179. /// The eBPF byte code instructions
  180. pub instructions: Vec<bpf_insn>,
  181. /// The function info
  182. pub func_info: FuncSecInfo,
  183. /// The line info
  184. pub line_info: LineSecInfo,
  185. /// Function info record size
  186. pub func_info_rec_size: usize,
  187. /// Line info record size
  188. pub line_info_rec_size: usize,
  189. }
  190. /// Section types containing eBPF programs
  191. ///
  192. /// # Section Name Parsing
  193. ///
  194. /// Section types are parsed from the section name strings.
  195. ///
  196. /// In order for Aya to treat a section as a [ProgramSection],
  197. /// there are a few requirements:
  198. /// - The section must be an executable code section.
  199. /// - The section name must conform to [Program Types and ELF Sections].
  200. ///
  201. /// [Program Types and ELF Sections]: https://docs.kernel.org/bpf/libbpf/program_types.html
  202. ///
  203. /// # Unsupported Sections
  204. ///
  205. /// Currently, the following section names are not supported yet:
  206. /// - `flow_dissector`: `BPF_PROG_TYPE_FLOW_DISSECTOR`
  207. /// - `ksyscall+` or `kretsyscall+`
  208. /// - `usdt+`
  209. /// - `kprobe.multi+` or `kretprobe.multi+`: `BPF_TRACE_KPROBE_MULTI`
  210. /// - `lsm_cgroup+`
  211. /// - `lwt_in`, `lwt_out`, `lwt_seg6local`, `lwt_xmit`
  212. /// - `raw_tp.w+`, `raw_tracepoint.w+`
  213. /// - `action`
  214. /// - `sk_reuseport/migrate`, `sk_reuseport`
  215. /// - `syscall`
  216. /// - `struct_ops+`
  217. /// - `fmod_ret+`, `fmod_ret.s+`
  218. /// - `iter+`, `iter.s+`
  219. #[derive(Debug, Clone)]
  220. #[allow(missing_docs)]
  221. pub enum ProgramSection {
  222. KRetProbe,
  223. KProbe,
  224. UProbe {
  225. sleepable: bool,
  226. },
  227. URetProbe {
  228. sleepable: bool,
  229. },
  230. TracePoint,
  231. SocketFilter,
  232. Xdp {
  233. frags: bool,
  234. attach_type: XdpAttachType,
  235. },
  236. SkMsg,
  237. SkSkbStreamParser,
  238. SkSkbStreamVerdict,
  239. SockOps,
  240. SchedClassifier,
  241. CgroupSkb,
  242. CgroupSkbIngress,
  243. CgroupSkbEgress,
  244. CgroupSockAddr {
  245. attach_type: CgroupSockAddrAttachType,
  246. },
  247. CgroupSysctl,
  248. CgroupSockopt {
  249. attach_type: CgroupSockoptAttachType,
  250. },
  251. LircMode2,
  252. PerfEvent,
  253. RawTracePoint,
  254. Lsm {
  255. sleepable: bool,
  256. },
  257. BtfTracePoint,
  258. FEntry {
  259. sleepable: bool,
  260. },
  261. FExit {
  262. sleepable: bool,
  263. },
  264. Extension,
  265. SkLookup,
  266. CgroupSock {
  267. attach_type: CgroupSockAttachType,
  268. },
  269. CgroupDevice,
  270. Iter {
  271. sleepable: bool,
  272. },
  273. }
  274. impl FromStr for ProgramSection {
  275. type Err = ParseError;
  276. fn from_str(section: &str) -> Result<ProgramSection, ParseError> {
  277. use ProgramSection::*;
  278. // parse the common case, eg "xdp/program_name" or
  279. // "sk_skb/stream_verdict/program_name"
  280. let mut pieces = section.split('/');
  281. let mut next = || {
  282. pieces
  283. .next()
  284. .ok_or_else(|| ParseError::InvalidProgramSection {
  285. section: section.to_owned(),
  286. })
  287. };
  288. let kind = next()?;
  289. Ok(match kind {
  290. "kprobe" => KProbe,
  291. "kretprobe" => KRetProbe,
  292. "uprobe" => UProbe { sleepable: false },
  293. "uprobe.s" => UProbe { sleepable: true },
  294. "uretprobe" => URetProbe { sleepable: false },
  295. "uretprobe.s" => URetProbe { sleepable: true },
  296. "xdp" | "xdp.frags" => Xdp {
  297. frags: kind == "xdp.frags",
  298. attach_type: match pieces.next() {
  299. None => XdpAttachType::Interface,
  300. Some("cpumap") => XdpAttachType::CpuMap,
  301. Some("devmap") => XdpAttachType::DevMap,
  302. Some(_) => {
  303. return Err(ParseError::InvalidProgramSection {
  304. section: section.to_owned(),
  305. })
  306. }
  307. },
  308. },
  309. "tp_btf" => BtfTracePoint,
  310. "tracepoint" | "tp" => TracePoint,
  311. "socket" => SocketFilter,
  312. "sk_msg" => SkMsg,
  313. "sk_skb" => {
  314. let name = next()?;
  315. match name {
  316. "stream_parser" => SkSkbStreamParser,
  317. "stream_verdict" => SkSkbStreamVerdict,
  318. _ => {
  319. return Err(ParseError::InvalidProgramSection {
  320. section: section.to_owned(),
  321. })
  322. }
  323. }
  324. }
  325. "sockops" => SockOps,
  326. "classifier" => SchedClassifier,
  327. "cgroup_skb" => {
  328. let name = next()?;
  329. match name {
  330. "ingress" => CgroupSkbIngress,
  331. "egress" => CgroupSkbEgress,
  332. _ => {
  333. return Err(ParseError::InvalidProgramSection {
  334. section: section.to_owned(),
  335. })
  336. }
  337. }
  338. }
  339. "cgroup" => {
  340. let name = next()?;
  341. match name {
  342. "skb" => CgroupSkb,
  343. "sysctl" => CgroupSysctl,
  344. "dev" => CgroupDevice,
  345. "getsockopt" => CgroupSockopt {
  346. attach_type: CgroupSockoptAttachType::Get,
  347. },
  348. "setsockopt" => CgroupSockopt {
  349. attach_type: CgroupSockoptAttachType::Set,
  350. },
  351. "sock" => CgroupSock {
  352. attach_type: CgroupSockAttachType::default(),
  353. },
  354. "post_bind4" => CgroupSock {
  355. attach_type: CgroupSockAttachType::PostBind4,
  356. },
  357. "post_bind6" => CgroupSock {
  358. attach_type: CgroupSockAttachType::PostBind6,
  359. },
  360. "sock_create" => CgroupSock {
  361. attach_type: CgroupSockAttachType::SockCreate,
  362. },
  363. "sock_release" => CgroupSock {
  364. attach_type: CgroupSockAttachType::SockRelease,
  365. },
  366. "bind4" => CgroupSockAddr {
  367. attach_type: CgroupSockAddrAttachType::Bind4,
  368. },
  369. "bind6" => CgroupSockAddr {
  370. attach_type: CgroupSockAddrAttachType::Bind6,
  371. },
  372. "connect4" => CgroupSockAddr {
  373. attach_type: CgroupSockAddrAttachType::Connect4,
  374. },
  375. "connect6" => CgroupSockAddr {
  376. attach_type: CgroupSockAddrAttachType::Connect6,
  377. },
  378. "getpeername4" => CgroupSockAddr {
  379. attach_type: CgroupSockAddrAttachType::GetPeerName4,
  380. },
  381. "getpeername6" => CgroupSockAddr {
  382. attach_type: CgroupSockAddrAttachType::GetPeerName6,
  383. },
  384. "getsockname4" => CgroupSockAddr {
  385. attach_type: CgroupSockAddrAttachType::GetSockName4,
  386. },
  387. "getsockname6" => CgroupSockAddr {
  388. attach_type: CgroupSockAddrAttachType::GetSockName6,
  389. },
  390. "sendmsg4" => CgroupSockAddr {
  391. attach_type: CgroupSockAddrAttachType::UDPSendMsg4,
  392. },
  393. "sendmsg6" => CgroupSockAddr {
  394. attach_type: CgroupSockAddrAttachType::UDPSendMsg6,
  395. },
  396. "recvmsg4" => CgroupSockAddr {
  397. attach_type: CgroupSockAddrAttachType::UDPRecvMsg4,
  398. },
  399. "recvmsg6" => CgroupSockAddr {
  400. attach_type: CgroupSockAddrAttachType::UDPRecvMsg6,
  401. },
  402. _ => {
  403. return Err(ParseError::InvalidProgramSection {
  404. section: section.to_owned(),
  405. });
  406. }
  407. }
  408. }
  409. "lirc_mode2" => LircMode2,
  410. "perf_event" => PerfEvent,
  411. "raw_tp" | "raw_tracepoint" => RawTracePoint,
  412. "lsm" => Lsm { sleepable: false },
  413. "lsm.s" => Lsm { sleepable: true },
  414. "fentry" => FEntry { sleepable: false },
  415. "fentry.s" => FEntry { sleepable: true },
  416. "fexit" => FExit { sleepable: false },
  417. "fexit.s" => FExit { sleepable: true },
  418. "freplace" => Extension,
  419. "sk_lookup" => SkLookup,
  420. "iter" => Iter { sleepable: false },
  421. "iter.s" => Iter { sleepable: true },
  422. _ => {
  423. return Err(ParseError::InvalidProgramSection {
  424. section: section.to_owned(),
  425. })
  426. }
  427. })
  428. }
  429. }
  430. impl Object {
  431. /// Parses the binary data as an object file into an [Object]
  432. pub fn parse(data: &[u8]) -> Result<Object, ParseError> {
  433. let obj = object::read::File::parse(data).map_err(ParseError::ElfError)?;
  434. let endianness = obj.endianness();
  435. let license = if let Some(section) = obj.section_by_name("license") {
  436. parse_license(Section::try_from(&section)?.data)?
  437. } else {
  438. CString::new("GPL").unwrap()
  439. };
  440. let kernel_version = if let Some(section) = obj.section_by_name("version") {
  441. parse_version(Section::try_from(&section)?.data, endianness)?
  442. } else {
  443. None
  444. };
  445. let mut bpf_obj = Object::new(endianness, license, kernel_version);
  446. if let Some(symbol_table) = obj.symbol_table() {
  447. for symbol in symbol_table.symbols() {
  448. let name = symbol
  449. .name()
  450. .ok()
  451. .map(String::from)
  452. .ok_or(BtfError::InvalidSymbolName)?;
  453. let sym = Symbol {
  454. index: symbol.index().0,
  455. name: Some(name.clone()),
  456. section_index: symbol.section().index().map(|i| i.0),
  457. address: symbol.address(),
  458. size: symbol.size(),
  459. is_definition: symbol.is_definition(),
  460. kind: symbol.kind(),
  461. };
  462. bpf_obj.symbol_table.insert(symbol.index().0, sym);
  463. if let Some(section_idx) = symbol.section().index() {
  464. bpf_obj
  465. .symbols_by_section
  466. .entry(section_idx)
  467. .or_default()
  468. .push(symbol.index().0);
  469. }
  470. if symbol.is_global() || symbol.kind() == SymbolKind::Data {
  471. bpf_obj.symbol_offset_by_name.insert(name, symbol.address());
  472. }
  473. }
  474. }
  475. // .BTF and .BTF.ext sections must be parsed first
  476. // as they're required to prepare function and line information
  477. // when parsing program sections
  478. if let Some(s) = obj.section_by_name(".BTF") {
  479. bpf_obj.parse_section(Section::try_from(&s)?)?;
  480. if let Some(s) = obj.section_by_name(".BTF.ext") {
  481. bpf_obj.parse_section(Section::try_from(&s)?)?;
  482. }
  483. }
  484. for s in obj.sections() {
  485. if let Ok(name) = s.name() {
  486. if name == ".BTF" || name == ".BTF.ext" {
  487. continue;
  488. }
  489. }
  490. bpf_obj.parse_section(Section::try_from(&s)?)?;
  491. }
  492. Ok(bpf_obj)
  493. }
  494. fn new(endianness: Endianness, license: CString, kernel_version: Option<u32>) -> Object {
  495. Object {
  496. endianness,
  497. license,
  498. kernel_version,
  499. btf: None,
  500. btf_ext: None,
  501. maps: HashMap::new(),
  502. programs: HashMap::new(),
  503. functions: BTreeMap::new(),
  504. relocations: HashMap::new(),
  505. symbol_table: HashMap::new(),
  506. symbols_by_section: HashMap::new(),
  507. section_infos: HashMap::new(),
  508. symbol_offset_by_name: HashMap::new(),
  509. }
  510. }
  511. /// Patches map data
  512. pub fn patch_map_data(
  513. &mut self,
  514. globals: HashMap<&str, (&[u8], bool)>,
  515. ) -> Result<(), ParseError> {
  516. let symbols: HashMap<String, &Symbol> = self
  517. .symbol_table
  518. .iter()
  519. .filter(|(_, s)| s.name.is_some())
  520. .map(|(_, s)| (s.name.as_ref().unwrap().clone(), s))
  521. .collect();
  522. for (name, (data, must_exist)) in globals {
  523. if let Some(symbol) = symbols.get(name) {
  524. if data.len() as u64 != symbol.size {
  525. return Err(ParseError::InvalidGlobalData {
  526. name: name.to_string(),
  527. sym_size: symbol.size,
  528. data_size: data.len(),
  529. });
  530. }
  531. let (_, map) = self
  532. .maps
  533. .iter_mut()
  534. // assumption: there is only one map created per section where we're trying to
  535. // patch data. this assumption holds true for the .rodata section at least
  536. .find(|(_, m)| symbol.section_index == Some(m.section_index()))
  537. .ok_or_else(|| ParseError::MapNotFound {
  538. index: symbol.section_index.unwrap_or(0),
  539. })?;
  540. let start = symbol.address as usize;
  541. let end = start + symbol.size as usize;
  542. if start > end || end > map.data().len() {
  543. return Err(ParseError::InvalidGlobalData {
  544. name: name.to_string(),
  545. sym_size: symbol.size,
  546. data_size: data.len(),
  547. });
  548. }
  549. map.data_mut().splice(start..end, data.iter().cloned());
  550. } else if must_exist {
  551. return Err(ParseError::SymbolNotFound {
  552. name: name.to_owned(),
  553. });
  554. }
  555. }
  556. Ok(())
  557. }
  558. fn parse_btf(&mut self, section: &Section) -> Result<(), BtfError> {
  559. self.btf = Some(Btf::parse(section.data, self.endianness)?);
  560. Ok(())
  561. }
  562. fn parse_btf_ext(&mut self, section: &Section) -> Result<(), BtfError> {
  563. self.btf_ext = Some(BtfExt::parse(
  564. section.data,
  565. self.endianness,
  566. self.btf.as_ref().unwrap(),
  567. )?);
  568. Ok(())
  569. }
  570. fn parse_programs(&mut self, section: &Section) -> Result<(), ParseError> {
  571. let program_section = ProgramSection::from_str(section.name)?;
  572. let syms =
  573. self.symbols_by_section
  574. .get(&section.index)
  575. .ok_or(ParseError::NoSymbolsForSection {
  576. section_name: section.name.to_string(),
  577. })?;
  578. for symbol_index in syms {
  579. let symbol = self
  580. .symbol_table
  581. .get(symbol_index)
  582. .expect("all symbols in symbols_by_section are also in symbol_table");
  583. // Here we get both ::Label (LBB*) and ::Text symbols, and we only want the latter.
  584. let name = match (symbol.name.as_ref(), symbol.kind) {
  585. (Some(name), SymbolKind::Text) if !name.is_empty() => name,
  586. _ => continue,
  587. };
  588. let (p, f) =
  589. self.parse_program(section, program_section.clone(), name.to_string(), symbol)?;
  590. let key = p.function_key();
  591. self.programs.insert(f.name.clone(), p);
  592. self.functions.insert(key, f);
  593. }
  594. Ok(())
  595. }
  596. fn parse_program(
  597. &self,
  598. section: &Section,
  599. program_section: ProgramSection,
  600. name: String,
  601. symbol: &Symbol,
  602. ) -> Result<(Program, Function), ParseError> {
  603. let offset = symbol.address as usize - section.address as usize;
  604. let (func_info, line_info, func_info_rec_size, line_info_rec_size) =
  605. get_func_and_line_info(self.btf_ext.as_ref(), symbol, section, offset, true);
  606. let start = symbol.address as usize;
  607. let end = (symbol.address + symbol.size) as usize;
  608. let function = Function {
  609. name: name.to_owned(),
  610. address: symbol.address,
  611. section_index: section.index,
  612. section_offset: start,
  613. instructions: copy_instructions(&section.data[start..end])?,
  614. func_info,
  615. line_info,
  616. func_info_rec_size,
  617. line_info_rec_size,
  618. };
  619. Ok((
  620. Program {
  621. license: self.license.clone(),
  622. kernel_version: self.kernel_version,
  623. section: program_section.clone(),
  624. section_index: section.index.0,
  625. address: symbol.address,
  626. },
  627. function,
  628. ))
  629. }
  630. fn parse_text_section(&mut self, section: Section) -> Result<(), ParseError> {
  631. let mut symbols_by_address = HashMap::new();
  632. for sym in self.symbol_table.values() {
  633. if sym.is_definition
  634. && sym.kind == SymbolKind::Text
  635. && sym.section_index == Some(section.index.0)
  636. {
  637. if symbols_by_address.contains_key(&sym.address) {
  638. return Err(ParseError::SymbolTableConflict {
  639. section_index: section.index.0,
  640. address: sym.address,
  641. });
  642. }
  643. symbols_by_address.insert(sym.address, sym);
  644. }
  645. }
  646. let mut offset = 0;
  647. while offset < section.data.len() {
  648. let address = section.address + offset as u64;
  649. let sym = symbols_by_address
  650. .get(&address)
  651. .ok_or(ParseError::UnknownSymbol {
  652. section_index: section.index.0,
  653. address,
  654. })?;
  655. if sym.size == 0 {
  656. return Err(ParseError::InvalidSymbol {
  657. index: sym.index,
  658. name: sym.name.clone(),
  659. });
  660. }
  661. let (func_info, line_info, func_info_rec_size, line_info_rec_size) =
  662. get_func_and_line_info(self.btf_ext.as_ref(), sym, &section, offset, false);
  663. self.functions.insert(
  664. (section.index.0, sym.address),
  665. Function {
  666. address,
  667. name: sym.name.clone().unwrap(),
  668. section_index: section.index,
  669. section_offset: offset,
  670. instructions: copy_instructions(
  671. &section.data[offset..offset + sym.size as usize],
  672. )?,
  673. func_info,
  674. line_info,
  675. func_info_rec_size,
  676. line_info_rec_size,
  677. },
  678. );
  679. offset += sym.size as usize;
  680. }
  681. if !section.relocations.is_empty() {
  682. self.relocations.insert(
  683. section.index,
  684. section
  685. .relocations
  686. .into_iter()
  687. .map(|rel| (rel.offset, rel))
  688. .collect(),
  689. );
  690. }
  691. Ok(())
  692. }
  693. fn parse_btf_maps(&mut self, section: &Section) -> Result<(), ParseError> {
  694. if self.btf.is_none() {
  695. return Err(ParseError::NoBTF);
  696. }
  697. let btf = self.btf.as_ref().unwrap();
  698. let maps: HashMap<&String, usize> = self
  699. .symbols_by_section
  700. .get(&section.index)
  701. .ok_or(ParseError::NoSymbolsForSection {
  702. section_name: section.name.to_owned(),
  703. })?
  704. .iter()
  705. .filter_map(|s| {
  706. let symbol = self.symbol_table.get(s).unwrap();
  707. symbol.name.as_ref().map(|name| (name, symbol.index))
  708. })
  709. .collect();
  710. for t in btf.types() {
  711. if let BtfType::DataSec(datasec) = &t {
  712. let type_name = match btf.type_name(t) {
  713. Ok(name) => name,
  714. _ => continue,
  715. };
  716. if type_name == section.name {
  717. // each btf_var_secinfo contains a map
  718. for info in &datasec.entries {
  719. let (map_name, def) = parse_btf_map_def(btf, info)?;
  720. let symbol_index =
  721. maps.get(&map_name)
  722. .ok_or_else(|| ParseError::SymbolNotFound {
  723. name: map_name.to_string(),
  724. })?;
  725. self.maps.insert(
  726. map_name,
  727. Map::Btf(BtfMap {
  728. def,
  729. section_index: section.index.0,
  730. symbol_index: *symbol_index,
  731. data: Vec::new(),
  732. }),
  733. );
  734. }
  735. }
  736. }
  737. }
  738. Ok(())
  739. }
  740. // Parses multiple map definition contained in a single `maps` section (which is
  741. // different from `.maps` which is used for BTF). We can tell where each map is
  742. // based on the symbol table.
  743. fn parse_maps_section<'a, I: Iterator<Item = &'a usize>>(
  744. &self,
  745. maps: &mut HashMap<String, Map>,
  746. section: &Section,
  747. symbols: I,
  748. ) -> Result<(), ParseError> {
  749. let mut have_symbols = false;
  750. // each symbol in the section is a separate map
  751. for i in symbols {
  752. let sym = self.symbol_table.get(i).ok_or(ParseError::SymbolNotFound {
  753. name: i.to_string(),
  754. })?;
  755. let start = sym.address as usize;
  756. let end = start + sym.size as usize;
  757. let data = &section.data[start..end];
  758. let name = sym
  759. .name
  760. .as_ref()
  761. .ok_or(ParseError::MapSymbolNameNotFound { i: *i })?;
  762. let def = parse_map_def(name, data)?;
  763. maps.insert(
  764. name.to_string(),
  765. Map::Legacy(LegacyMap {
  766. section_index: section.index.0,
  767. section_kind: section.kind,
  768. symbol_index: Some(sym.index),
  769. def,
  770. data: Vec::new(),
  771. }),
  772. );
  773. have_symbols = true;
  774. }
  775. if !have_symbols {
  776. return Err(ParseError::NoSymbolsForSection {
  777. section_name: section.name.to_owned(),
  778. });
  779. }
  780. Ok(())
  781. }
  782. fn parse_section(&mut self, section: Section) -> Result<(), ParseError> {
  783. self.section_infos
  784. .insert(section.name.to_owned(), (section.index, section.size));
  785. match section.kind {
  786. EbpfSectionKind::Data | EbpfSectionKind::Rodata | EbpfSectionKind::Bss => {
  787. self.maps
  788. .insert(section.name.to_string(), parse_data_map_section(&section)?);
  789. }
  790. EbpfSectionKind::Text => self.parse_text_section(section)?,
  791. EbpfSectionKind::Btf => self.parse_btf(&section)?,
  792. EbpfSectionKind::BtfExt => self.parse_btf_ext(&section)?,
  793. EbpfSectionKind::BtfMaps => self.parse_btf_maps(&section)?,
  794. EbpfSectionKind::Maps => {
  795. // take out self.maps so we can borrow the iterator below
  796. // without cloning or collecting
  797. let mut maps = mem::take(&mut self.maps);
  798. // extract the symbols for the .maps section, we'll need them
  799. // during parsing
  800. let symbols = self
  801. .symbols_by_section
  802. .get(&section.index)
  803. .ok_or(ParseError::NoSymbolsForSection {
  804. section_name: section.name.to_owned(),
  805. })?
  806. .iter();
  807. let res = self.parse_maps_section(&mut maps, &section, symbols);
  808. // put the maps back
  809. self.maps = maps;
  810. res?
  811. }
  812. EbpfSectionKind::Program => {
  813. self.parse_programs(&section)?;
  814. if !section.relocations.is_empty() {
  815. self.relocations.insert(
  816. section.index,
  817. section
  818. .relocations
  819. .into_iter()
  820. .map(|rel| (rel.offset, rel))
  821. .collect(),
  822. );
  823. }
  824. }
  825. EbpfSectionKind::Undefined | EbpfSectionKind::License | EbpfSectionKind::Version => {}
  826. }
  827. Ok(())
  828. }
  829. /// Sanitize BPF functions.
  830. pub fn sanitize_functions(&mut self, features: &Features) {
  831. for function in self.functions.values_mut() {
  832. function.sanitize(features);
  833. }
  834. }
  835. }
  836. fn insn_is_helper_call(ins: &bpf_insn) -> bool {
  837. let klass = (ins.code & 0x07) as u32;
  838. let op = (ins.code & 0xF0) as u32;
  839. let src = (ins.code & 0x08) as u32;
  840. klass == BPF_JMP && op == BPF_CALL && src == BPF_K && ins.src_reg() == 0 && ins.dst_reg() == 0
  841. }
  842. impl Function {
  843. fn sanitize(&mut self, features: &Features) {
  844. for inst in &mut self.instructions {
  845. if !insn_is_helper_call(inst) {
  846. continue;
  847. }
  848. if !features.bpf_probe_read_kernel {
  849. const BPF_FUNC_PROBE_READ_KERNEL: i32 = BPF_FUNC_probe_read_kernel as i32;
  850. const BPF_FUNC_PROBE_READ_USER: i32 = BPF_FUNC_probe_read_user as i32;
  851. const BPF_FUNC_PROBE_READ: i32 = BPF_FUNC_probe_read as i32;
  852. const BPF_FUNC_PROBE_READ_KERNEL_STR: i32 = BPF_FUNC_probe_read_kernel_str as i32;
  853. const BPF_FUNC_PROBE_READ_USER_STR: i32 = BPF_FUNC_probe_read_user_str as i32;
  854. const BPF_FUNC_PROBE_READ_STR: i32 = BPF_FUNC_probe_read_str as i32;
  855. match inst.imm {
  856. BPF_FUNC_PROBE_READ_KERNEL | BPF_FUNC_PROBE_READ_USER => {
  857. inst.imm = BPF_FUNC_PROBE_READ;
  858. }
  859. BPF_FUNC_PROBE_READ_KERNEL_STR | BPF_FUNC_PROBE_READ_USER_STR => {
  860. inst.imm = BPF_FUNC_PROBE_READ_STR;
  861. }
  862. _ => {}
  863. }
  864. }
  865. }
  866. }
  867. }
  868. /// Errors caught during parsing the object file
  869. #[derive(Debug, thiserror::Error)]
  870. #[allow(missing_docs)]
  871. pub enum ParseError {
  872. #[error("error parsing ELF data")]
  873. ElfError(object::read::Error),
  874. /// Error parsing BTF object
  875. #[error("BTF error")]
  876. BtfError(#[from] BtfError),
  877. #[error("invalid license `{data:?}`: missing NULL terminator")]
  878. MissingLicenseNullTerminator { data: Vec<u8> },
  879. #[error("invalid license `{data:?}`")]
  880. InvalidLicense { data: Vec<u8> },
  881. #[error("invalid kernel version `{data:?}`")]
  882. InvalidKernelVersion { data: Vec<u8> },
  883. #[error("error parsing section with index {index}")]
  884. SectionError {
  885. index: usize,
  886. error: object::read::Error,
  887. },
  888. #[error("unsupported relocation target")]
  889. UnsupportedRelocationTarget,
  890. #[error("invalid program section `{section}`")]
  891. InvalidProgramSection { section: String },
  892. #[error("invalid program code")]
  893. InvalidProgramCode,
  894. #[error("error parsing map `{name}`")]
  895. InvalidMapDefinition { name: String },
  896. #[error("two or more symbols in section `{section_index}` have the same address {address:#X}")]
  897. SymbolTableConflict { section_index: usize, address: u64 },
  898. #[error("unknown symbol in section `{section_index}` at address {address:#X}")]
  899. UnknownSymbol { section_index: usize, address: u64 },
  900. #[error("invalid symbol, index `{index}` name: {}", .name.as_ref().unwrap_or(&"[unknown]".into()))]
  901. InvalidSymbol { index: usize, name: Option<String> },
  902. #[error("symbol {name} has size `{sym_size}`, but provided data is of size `{data_size}`")]
  903. InvalidGlobalData {
  904. name: String,
  905. sym_size: u64,
  906. data_size: usize,
  907. },
  908. #[error("symbol with name {name} not found in the symbols table")]
  909. SymbolNotFound { name: String },
  910. #[error("map for section with index {index} not found")]
  911. MapNotFound { index: usize },
  912. #[error("the map number {i} in the `maps` section doesn't have a symbol name")]
  913. MapSymbolNameNotFound { i: usize },
  914. #[error("no symbols found in the {section_name} section")]
  915. NoSymbolsForSection { section_name: String },
  916. /// No BTF parsed for object
  917. #[error("no BTF parsed for object")]
  918. NoBTF,
  919. }
  920. /// Invalid bindings to the bpf type from the parsed/received value.
  921. pub struct InvalidTypeBinding<T> {
  922. /// The value parsed/received.
  923. pub value: T,
  924. }
  925. /// The kind of an ELF section.
  926. #[derive(Debug, Copy, Clone, Eq, PartialEq)]
  927. pub enum EbpfSectionKind {
  928. /// Undefined
  929. Undefined,
  930. /// `maps`
  931. Maps,
  932. /// `.maps`
  933. BtfMaps,
  934. /// A program section
  935. Program,
  936. /// `.data`
  937. Data,
  938. /// `.rodata`
  939. Rodata,
  940. /// `.bss`
  941. Bss,
  942. /// `.text`
  943. Text,
  944. /// `.BTF`
  945. Btf,
  946. /// `.BTF.ext`
  947. BtfExt,
  948. /// `license`
  949. License,
  950. /// `version`
  951. Version,
  952. }
  953. impl EbpfSectionKind {
  954. fn from_name(name: &str) -> EbpfSectionKind {
  955. if name.starts_with("license") {
  956. EbpfSectionKind::License
  957. } else if name.starts_with("version") {
  958. EbpfSectionKind::Version
  959. } else if name.starts_with("maps") {
  960. EbpfSectionKind::Maps
  961. } else if name.starts_with(".maps") {
  962. EbpfSectionKind::BtfMaps
  963. } else if name.starts_with(".text") {
  964. EbpfSectionKind::Text
  965. } else if name.starts_with(".bss") {
  966. EbpfSectionKind::Bss
  967. } else if name.starts_with(".data") {
  968. EbpfSectionKind::Data
  969. } else if name.starts_with(".rodata") {
  970. EbpfSectionKind::Rodata
  971. } else if name == ".BTF" {
  972. EbpfSectionKind::Btf
  973. } else if name == ".BTF.ext" {
  974. EbpfSectionKind::BtfExt
  975. } else {
  976. EbpfSectionKind::Undefined
  977. }
  978. }
  979. }
  980. #[derive(Debug)]
  981. struct Section<'a> {
  982. index: SectionIndex,
  983. kind: EbpfSectionKind,
  984. address: u64,
  985. name: &'a str,
  986. data: &'a [u8],
  987. size: u64,
  988. relocations: Vec<Relocation>,
  989. }
  990. impl<'a> TryFrom<&'a ObjSection<'_, '_>> for Section<'a> {
  991. type Error = ParseError;
  992. fn try_from(section: &'a ObjSection) -> Result<Section<'a>, ParseError> {
  993. let index = section.index();
  994. let map_err = |error| ParseError::SectionError {
  995. index: index.0,
  996. error,
  997. };
  998. let name = section.name().map_err(map_err)?;
  999. let kind = match EbpfSectionKind::from_name(name) {
  1000. EbpfSectionKind::Undefined => {
  1001. if section.kind() == SectionKind::Text && section.size() > 0 {
  1002. EbpfSectionKind::Program
  1003. } else {
  1004. EbpfSectionKind::Undefined
  1005. }
  1006. }
  1007. k => k,
  1008. };
  1009. Ok(Section {
  1010. index,
  1011. kind,
  1012. address: section.address(),
  1013. name,
  1014. data: section.data().map_err(map_err)?,
  1015. size: section.size(),
  1016. relocations: section
  1017. .relocations()
  1018. .map(|(offset, r)| {
  1019. Ok(Relocation {
  1020. symbol_index: match r.target() {
  1021. RelocationTarget::Symbol(index) => index.0,
  1022. _ => return Err(ParseError::UnsupportedRelocationTarget),
  1023. },
  1024. offset,
  1025. size: r.size(),
  1026. })
  1027. })
  1028. .collect::<Result<Vec<_>, _>>()?,
  1029. })
  1030. }
  1031. }
  1032. fn parse_license(data: &[u8]) -> Result<CString, ParseError> {
  1033. if data.len() < 2 {
  1034. return Err(ParseError::InvalidLicense {
  1035. data: data.to_vec(),
  1036. });
  1037. }
  1038. if data[data.len() - 1] != 0 {
  1039. return Err(ParseError::MissingLicenseNullTerminator {
  1040. data: data.to_vec(),
  1041. });
  1042. }
  1043. Ok(CStr::from_bytes_with_nul(data)
  1044. .map_err(|_| ParseError::InvalidLicense {
  1045. data: data.to_vec(),
  1046. })?
  1047. .to_owned())
  1048. }
  1049. fn parse_version(data: &[u8], endianness: object::Endianness) -> Result<Option<u32>, ParseError> {
  1050. let data = match data.len() {
  1051. 4 => data.try_into().unwrap(),
  1052. _ => {
  1053. return Err(ParseError::InvalidKernelVersion {
  1054. data: data.to_vec(),
  1055. })
  1056. }
  1057. };
  1058. let v = match endianness {
  1059. object::Endianness::Big => u32::from_be_bytes(data),
  1060. object::Endianness::Little => u32::from_le_bytes(data),
  1061. };
  1062. Ok(if v == KERNEL_VERSION_ANY {
  1063. None
  1064. } else {
  1065. Some(v)
  1066. })
  1067. }
  1068. // Gets an integer value from a BTF map defintion K/V pair.
  1069. // type_id should be a PTR to an ARRAY.
  1070. // the value is encoded in the array nr_elems field.
  1071. fn get_map_field(btf: &Btf, type_id: u32) -> Result<u32, BtfError> {
  1072. let pty = match &btf.type_by_id(type_id)? {
  1073. BtfType::Ptr(pty) => pty,
  1074. other => {
  1075. return Err(BtfError::UnexpectedBtfType {
  1076. type_id: other.btf_type().unwrap_or(0),
  1077. })
  1078. }
  1079. };
  1080. // Safety: union
  1081. let arr = match &btf.type_by_id(pty.btf_type)? {
  1082. BtfType::Array(Array { array, .. }) => array,
  1083. other => {
  1084. return Err(BtfError::UnexpectedBtfType {
  1085. type_id: other.btf_type().unwrap_or(0),
  1086. })
  1087. }
  1088. };
  1089. Ok(arr.len)
  1090. }
  1091. // Parsed '.bss' '.data' and '.rodata' sections. These sections are arrays of
  1092. // bytes and are relocated based on their section index.
  1093. fn parse_data_map_section(section: &Section) -> Result<Map, ParseError> {
  1094. let (def, data) = match section.kind {
  1095. EbpfSectionKind::Data | EbpfSectionKind::Rodata => {
  1096. let def = bpf_map_def {
  1097. map_type: BPF_MAP_TYPE_ARRAY as u32,
  1098. key_size: mem::size_of::<u32>() as u32,
  1099. // We need to use section.size here since
  1100. // .bss will always have data.len() == 0
  1101. value_size: section.size as u32,
  1102. max_entries: 1,
  1103. map_flags: if section.kind == EbpfSectionKind::Rodata {
  1104. BPF_F_RDONLY_PROG
  1105. } else {
  1106. 0
  1107. },
  1108. ..Default::default()
  1109. };
  1110. (def, section.data.to_vec())
  1111. }
  1112. EbpfSectionKind::Bss => {
  1113. let def = bpf_map_def {
  1114. map_type: BPF_MAP_TYPE_ARRAY as u32,
  1115. key_size: mem::size_of::<u32>() as u32,
  1116. value_size: section.size as u32,
  1117. max_entries: 1,
  1118. map_flags: 0,
  1119. ..Default::default()
  1120. };
  1121. (def, vec![0; section.size as usize])
  1122. }
  1123. _ => unreachable!(),
  1124. };
  1125. Ok(Map::Legacy(LegacyMap {
  1126. section_index: section.index.0,
  1127. section_kind: section.kind,
  1128. // Data maps don't require symbols to be relocated
  1129. symbol_index: None,
  1130. def,
  1131. data,
  1132. }))
  1133. }
  1134. fn parse_map_def(name: &str, data: &[u8]) -> Result<bpf_map_def, ParseError> {
  1135. if data.len() < MINIMUM_MAP_SIZE {
  1136. return Err(ParseError::InvalidMapDefinition {
  1137. name: name.to_owned(),
  1138. });
  1139. }
  1140. if data.len() < mem::size_of::<bpf_map_def>() {
  1141. let mut map_def = bpf_map_def::default();
  1142. unsafe {
  1143. let map_def_ptr =
  1144. from_raw_parts_mut(&mut map_def as *mut bpf_map_def as *mut u8, data.len());
  1145. map_def_ptr.copy_from_slice(data);
  1146. }
  1147. Ok(map_def)
  1148. } else {
  1149. Ok(unsafe { ptr::read_unaligned(data.as_ptr() as *const bpf_map_def) })
  1150. }
  1151. }
  1152. fn parse_btf_map_def(btf: &Btf, info: &DataSecEntry) -> Result<(String, BtfMapDef), BtfError> {
  1153. let ty = match btf.type_by_id(info.btf_type)? {
  1154. BtfType::Var(var) => var,
  1155. other => {
  1156. return Err(BtfError::UnexpectedBtfType {
  1157. type_id: other.btf_type().unwrap_or(0),
  1158. })
  1159. }
  1160. };
  1161. let map_name = btf.string_at(ty.name_offset)?;
  1162. let mut map_def = BtfMapDef::default();
  1163. // Safety: union
  1164. let root_type = btf.resolve_type(ty.btf_type)?;
  1165. let s = match btf.type_by_id(root_type)? {
  1166. BtfType::Struct(s) => s,
  1167. other => {
  1168. return Err(BtfError::UnexpectedBtfType {
  1169. type_id: other.btf_type().unwrap_or(0),
  1170. })
  1171. }
  1172. };
  1173. for m in &s.members {
  1174. match btf.string_at(m.name_offset)?.as_ref() {
  1175. "type" => {
  1176. map_def.map_type = get_map_field(btf, m.btf_type)?;
  1177. }
  1178. "key" => {
  1179. if let BtfType::Ptr(pty) = btf.type_by_id(m.btf_type)? {
  1180. // Safety: union
  1181. let t = pty.btf_type;
  1182. map_def.key_size = btf.type_size(t)? as u32;
  1183. map_def.btf_key_type_id = t;
  1184. } else {
  1185. return Err(BtfError::UnexpectedBtfType {
  1186. type_id: m.btf_type,
  1187. });
  1188. }
  1189. }
  1190. "key_size" => {
  1191. map_def.key_size = get_map_field(btf, m.btf_type)?;
  1192. }
  1193. "value" => {
  1194. if let BtfType::Ptr(pty) = btf.type_by_id(m.btf_type)? {
  1195. let t = pty.btf_type;
  1196. map_def.value_size = btf.type_size(t)? as u32;
  1197. map_def.btf_value_type_id = t;
  1198. } else {
  1199. return Err(BtfError::UnexpectedBtfType {
  1200. type_id: m.btf_type,
  1201. });
  1202. }
  1203. }
  1204. "value_size" => {
  1205. map_def.value_size = get_map_field(btf, m.btf_type)?;
  1206. }
  1207. "max_entries" => {
  1208. map_def.max_entries = get_map_field(btf, m.btf_type)?;
  1209. }
  1210. "map_flags" => {
  1211. map_def.map_flags = get_map_field(btf, m.btf_type)?;
  1212. }
  1213. "pinning" => {
  1214. let pinning = get_map_field(btf, m.btf_type)?;
  1215. map_def.pinning = PinningType::try_from(pinning).unwrap_or_else(|_| {
  1216. debug!("{} is not a valid pin type. using PIN_NONE", pinning);
  1217. PinningType::None
  1218. });
  1219. }
  1220. other => {
  1221. debug!("skipping unknown map section: {}", other);
  1222. continue;
  1223. }
  1224. }
  1225. }
  1226. Ok((map_name.to_string(), map_def))
  1227. }
  1228. /// Parses a [bpf_map_info] into a [Map].
  1229. pub fn parse_map_info(info: bpf_map_info, pinned: PinningType) -> Map {
  1230. if info.btf_key_type_id != 0 {
  1231. Map::Btf(BtfMap {
  1232. def: BtfMapDef {
  1233. map_type: info.type_,
  1234. key_size: info.key_size,
  1235. value_size: info.value_size,
  1236. max_entries: info.max_entries,
  1237. map_flags: info.map_flags,
  1238. pinning: pinned,
  1239. btf_key_type_id: info.btf_key_type_id,
  1240. btf_value_type_id: info.btf_value_type_id,
  1241. },
  1242. section_index: 0,
  1243. symbol_index: 0,
  1244. data: Vec::new(),
  1245. })
  1246. } else {
  1247. Map::Legacy(LegacyMap {
  1248. def: bpf_map_def {
  1249. map_type: info.type_,
  1250. key_size: info.key_size,
  1251. value_size: info.value_size,
  1252. max_entries: info.max_entries,
  1253. map_flags: info.map_flags,
  1254. pinning: pinned,
  1255. id: info.id,
  1256. },
  1257. section_index: 0,
  1258. symbol_index: None,
  1259. section_kind: EbpfSectionKind::Undefined,
  1260. data: Vec::new(),
  1261. })
  1262. }
  1263. }
  1264. /// Copies a block of eBPF instructions
  1265. pub fn copy_instructions(data: &[u8]) -> Result<Vec<bpf_insn>, ParseError> {
  1266. if data.len() % mem::size_of::<bpf_insn>() > 0 {
  1267. return Err(ParseError::InvalidProgramCode);
  1268. }
  1269. let instructions = data
  1270. .chunks_exact(mem::size_of::<bpf_insn>())
  1271. .map(|d| unsafe { ptr::read_unaligned(d.as_ptr() as *const bpf_insn) })
  1272. .collect::<Vec<_>>();
  1273. Ok(instructions)
  1274. }
  1275. fn get_func_and_line_info(
  1276. btf_ext: Option<&BtfExt>,
  1277. symbol: &Symbol,
  1278. section: &Section,
  1279. offset: usize,
  1280. rewrite_insn_off: bool,
  1281. ) -> (FuncSecInfo, LineSecInfo, usize, usize) {
  1282. btf_ext
  1283. .map(|btf_ext| {
  1284. let instruction_offset = (offset / INS_SIZE) as u32;
  1285. let symbol_size_instructions = (symbol.size as usize / INS_SIZE) as u32;
  1286. let mut func_info = btf_ext.func_info.get(section.name);
  1287. func_info.func_info.retain_mut(|f| {
  1288. let retain = f.insn_off == instruction_offset;
  1289. if retain && rewrite_insn_off {
  1290. f.insn_off = 0;
  1291. }
  1292. retain
  1293. });
  1294. let mut line_info = btf_ext.line_info.get(section.name);
  1295. line_info
  1296. .line_info
  1297. .retain_mut(|l| match l.insn_off.checked_sub(instruction_offset) {
  1298. None => false,
  1299. Some(insn_off) => {
  1300. let retain = insn_off < symbol_size_instructions;
  1301. if retain && rewrite_insn_off {
  1302. l.insn_off = insn_off
  1303. }
  1304. retain
  1305. }
  1306. });
  1307. (
  1308. func_info,
  1309. line_info,
  1310. btf_ext.func_info_rec_size(),
  1311. btf_ext.line_info_rec_size(),
  1312. )
  1313. })
  1314. .unwrap_or_default()
  1315. }
  1316. #[cfg(test)]
  1317. mod tests {
  1318. use alloc::vec;
  1319. use assert_matches::assert_matches;
  1320. use super::*;
  1321. use crate::generated::btf_ext_header;
  1322. const FAKE_INS_LEN: u64 = 8;
  1323. fn fake_section<'a>(
  1324. kind: EbpfSectionKind,
  1325. name: &'a str,
  1326. data: &'a [u8],
  1327. index: Option<usize>,
  1328. ) -> Section<'a> {
  1329. let idx = index.unwrap_or(0);
  1330. Section {
  1331. index: SectionIndex(idx),
  1332. kind,
  1333. address: 0,
  1334. name,
  1335. data,
  1336. size: data.len() as u64,
  1337. relocations: Vec::new(),
  1338. }
  1339. }
  1340. fn fake_ins() -> bpf_insn {
  1341. bpf_insn {
  1342. code: 0,
  1343. _bitfield_align_1: [],
  1344. _bitfield_1: bpf_insn::new_bitfield_1(0, 0),
  1345. off: 0,
  1346. imm: 0,
  1347. }
  1348. }
  1349. fn fake_sym(obj: &mut Object, section_index: usize, address: u64, name: &str, size: u64) {
  1350. let idx = obj.symbol_table.len();
  1351. obj.symbol_table.insert(
  1352. idx + 1,
  1353. Symbol {
  1354. index: idx + 1,
  1355. section_index: Some(section_index),
  1356. name: Some(name.to_string()),
  1357. address,
  1358. size,
  1359. is_definition: false,
  1360. kind: SymbolKind::Text,
  1361. },
  1362. );
  1363. obj.symbols_by_section
  1364. .entry(SectionIndex(section_index))
  1365. .or_default()
  1366. .push(idx + 1);
  1367. }
  1368. fn bytes_of<T>(val: &T) -> &[u8] {
  1369. // Safety: This is for testing only
  1370. unsafe { crate::util::bytes_of(val) }
  1371. }
  1372. #[test]
  1373. fn test_parse_generic_error() {
  1374. assert_matches!(Object::parse(&b"foo"[..]), Err(ParseError::ElfError(_)))
  1375. }
  1376. #[test]
  1377. fn test_parse_license() {
  1378. assert_matches!(parse_license(b""), Err(ParseError::InvalidLicense { .. }));
  1379. assert_matches!(parse_license(b"\0"), Err(ParseError::InvalidLicense { .. }));
  1380. assert_matches!(
  1381. parse_license(b"GPL"),
  1382. Err(ParseError::MissingLicenseNullTerminator { .. })
  1383. );
  1384. assert_eq!(parse_license(b"GPL\0").unwrap().to_str().unwrap(), "GPL");
  1385. }
  1386. #[test]
  1387. fn test_parse_version() {
  1388. assert_matches!(
  1389. parse_version(b"", Endianness::Little),
  1390. Err(ParseError::InvalidKernelVersion { .. })
  1391. );
  1392. assert_matches!(
  1393. parse_version(b"123", Endianness::Little),
  1394. Err(ParseError::InvalidKernelVersion { .. })
  1395. );
  1396. assert_matches!(
  1397. parse_version(&0xFFFF_FFFEu32.to_le_bytes(), Endianness::Little),
  1398. Ok(None)
  1399. );
  1400. assert_matches!(
  1401. parse_version(&0xFFFF_FFFEu32.to_be_bytes(), Endianness::Big),
  1402. Ok(None)
  1403. );
  1404. assert_matches!(
  1405. parse_version(&1234u32.to_le_bytes(), Endianness::Little),
  1406. Ok(Some(1234))
  1407. );
  1408. }
  1409. #[test]
  1410. fn test_parse_map_def_error() {
  1411. assert_matches!(
  1412. parse_map_def("foo", &[]),
  1413. Err(ParseError::InvalidMapDefinition { .. })
  1414. );
  1415. }
  1416. #[test]
  1417. fn test_parse_map_short() {
  1418. let def = bpf_map_def {
  1419. map_type: 1,
  1420. key_size: 2,
  1421. value_size: 3,
  1422. max_entries: 4,
  1423. map_flags: 5,
  1424. id: 0,
  1425. pinning: PinningType::None,
  1426. };
  1427. assert_eq!(
  1428. parse_map_def("foo", &bytes_of(&def)[..MINIMUM_MAP_SIZE]).unwrap(),
  1429. def
  1430. );
  1431. }
  1432. #[test]
  1433. fn test_parse_map_def() {
  1434. let def = bpf_map_def {
  1435. map_type: 1,
  1436. key_size: 2,
  1437. value_size: 3,
  1438. max_entries: 4,
  1439. map_flags: 5,
  1440. id: 6,
  1441. pinning: PinningType::ByName,
  1442. };
  1443. assert_eq!(parse_map_def("foo", bytes_of(&def)).unwrap(), def);
  1444. }
  1445. #[test]
  1446. fn test_parse_map_def_with_padding() {
  1447. let def = bpf_map_def {
  1448. map_type: 1,
  1449. key_size: 2,
  1450. value_size: 3,
  1451. max_entries: 4,
  1452. map_flags: 5,
  1453. id: 6,
  1454. pinning: PinningType::ByName,
  1455. };
  1456. let mut buf = [0u8; 128];
  1457. unsafe { ptr::write_unaligned(buf.as_mut_ptr() as *mut _, def) };
  1458. assert_eq!(parse_map_def("foo", &buf).unwrap(), def);
  1459. }
  1460. #[test]
  1461. fn test_parse_map_data() {
  1462. let map_data = b"map data";
  1463. assert_matches!(
  1464. parse_data_map_section(
  1465. &fake_section(
  1466. EbpfSectionKind::Data,
  1467. ".bss",
  1468. map_data,
  1469. None,
  1470. ),
  1471. ),
  1472. Ok(Map::Legacy(LegacyMap {
  1473. section_index: 0,
  1474. section_kind: EbpfSectionKind::Data,
  1475. symbol_index: None,
  1476. def: bpf_map_def {
  1477. map_type: _map_type,
  1478. key_size: 4,
  1479. value_size,
  1480. max_entries: 1,
  1481. map_flags: 0,
  1482. id: 0,
  1483. pinning: PinningType::None,
  1484. },
  1485. data,
  1486. })) if data == map_data && value_size == map_data.len() as u32
  1487. )
  1488. }
  1489. fn fake_obj() -> Object {
  1490. Object::new(Endianness::Little, CString::new("GPL").unwrap(), None)
  1491. }
  1492. #[test]
  1493. fn sanitizes_empty_btf_files_to_none() {
  1494. let mut obj = fake_obj();
  1495. let btf = Btf::new();
  1496. let btf_bytes = btf.to_bytes();
  1497. obj.parse_section(fake_section(EbpfSectionKind::Btf, ".BTF", &btf_bytes, None))
  1498. .unwrap();
  1499. const FUNC_INFO_LEN: u32 = 4;
  1500. const LINE_INFO_LEN: u32 = 4;
  1501. const CORE_RELO_LEN: u32 = 16;
  1502. let ext_header = btf_ext_header {
  1503. magic: 0xeb9f,
  1504. version: 1,
  1505. flags: 0,
  1506. hdr_len: 24,
  1507. func_info_off: 0,
  1508. func_info_len: FUNC_INFO_LEN,
  1509. line_info_off: FUNC_INFO_LEN,
  1510. line_info_len: LINE_INFO_LEN,
  1511. core_relo_off: FUNC_INFO_LEN + LINE_INFO_LEN,
  1512. core_relo_len: CORE_RELO_LEN,
  1513. };
  1514. let btf_ext_bytes = bytes_of::<btf_ext_header>(&ext_header).to_vec();
  1515. obj.parse_section(fake_section(
  1516. EbpfSectionKind::BtfExt,
  1517. ".BTF.ext",
  1518. &btf_ext_bytes,
  1519. None,
  1520. ))
  1521. .unwrap();
  1522. let btf = obj.fixup_and_sanitize_btf(&BtfFeatures::default()).unwrap();
  1523. assert!(btf.is_none());
  1524. }
  1525. #[test]
  1526. fn test_parse_program_error() {
  1527. let mut obj = fake_obj();
  1528. fake_sym(&mut obj, 0, 0, "foo", 1);
  1529. assert_matches!(
  1530. obj.parse_programs(&fake_section(
  1531. EbpfSectionKind::Program,
  1532. "kprobe/foo",
  1533. &42u32.to_ne_bytes(),
  1534. None,
  1535. ),),
  1536. Err(ParseError::InvalidProgramCode)
  1537. );
  1538. }
  1539. #[test]
  1540. fn test_parse_program() {
  1541. let mut obj = fake_obj();
  1542. fake_sym(&mut obj, 0, 0, "foo", FAKE_INS_LEN);
  1543. obj.parse_programs(&fake_section(
  1544. EbpfSectionKind::Program,
  1545. "kprobe/foo",
  1546. bytes_of(&fake_ins()),
  1547. None,
  1548. ))
  1549. .unwrap();
  1550. let prog_foo = obj.programs.get("foo").unwrap();
  1551. assert_matches!(prog_foo, Program {
  1552. license,
  1553. kernel_version: None,
  1554. section: ProgramSection::KProbe,
  1555. ..
  1556. } => assert_eq!(license.to_str().unwrap(), "GPL"));
  1557. assert_matches!(
  1558. obj.functions.get(&prog_foo.function_key()),
  1559. Some(Function {
  1560. name,
  1561. address: 0,
  1562. section_index: SectionIndex(0),
  1563. section_offset: 0,
  1564. instructions,
  1565. ..}) if name == "foo" && instructions.len() == 1
  1566. )
  1567. }
  1568. #[test]
  1569. fn test_parse_section_map() {
  1570. let mut obj = fake_obj();
  1571. fake_sym(&mut obj, 0, 0, "foo", mem::size_of::<bpf_map_def>() as u64);
  1572. assert_matches!(
  1573. obj.parse_section(fake_section(
  1574. EbpfSectionKind::Maps,
  1575. "maps/foo",
  1576. bytes_of(&bpf_map_def {
  1577. map_type: 1,
  1578. key_size: 2,
  1579. value_size: 3,
  1580. max_entries: 4,
  1581. map_flags: 5,
  1582. ..Default::default()
  1583. }),
  1584. None,
  1585. )),
  1586. Ok(())
  1587. );
  1588. assert!(obj.maps.contains_key("foo"));
  1589. }
  1590. #[test]
  1591. fn test_parse_multiple_program_in_same_section() {
  1592. let mut obj = fake_obj();
  1593. fake_sym(&mut obj, 0, 0, "foo", FAKE_INS_LEN);
  1594. fake_sym(&mut obj, 0, FAKE_INS_LEN, "bar", FAKE_INS_LEN);
  1595. let insns = [fake_ins(), fake_ins()];
  1596. let data = bytes_of(&insns);
  1597. obj.parse_programs(&fake_section(
  1598. EbpfSectionKind::Program,
  1599. "kprobe",
  1600. data,
  1601. None,
  1602. ))
  1603. .unwrap();
  1604. let prog_foo = obj.programs.get("foo").unwrap();
  1605. let function_foo = obj.functions.get(&prog_foo.function_key()).unwrap();
  1606. let prog_bar = obj.programs.get("bar").unwrap();
  1607. let function_bar = obj.functions.get(&prog_bar.function_key()).unwrap();
  1608. assert_matches!(prog_foo, Program {
  1609. license,
  1610. kernel_version: None,
  1611. section: ProgramSection::KProbe,
  1612. ..
  1613. } => assert_eq!(license.to_str().unwrap(), "GPL"));
  1614. assert_matches!(
  1615. function_foo,
  1616. Function {
  1617. name,
  1618. address: 0,
  1619. section_index: SectionIndex(0),
  1620. section_offset: 0,
  1621. instructions,
  1622. ..
  1623. } if name == "foo" && instructions.len() == 1
  1624. );
  1625. assert_matches!(prog_bar, Program {
  1626. license,
  1627. kernel_version: None,
  1628. section: ProgramSection::KProbe ,
  1629. ..
  1630. } => assert_eq!(license.to_str().unwrap(), "GPL"));
  1631. assert_matches!(
  1632. function_bar,
  1633. Function {
  1634. name,
  1635. address: 8,
  1636. section_index: SectionIndex(0),
  1637. section_offset: 8,
  1638. instructions,
  1639. ..
  1640. } if name == "bar" && instructions.len() == 1
  1641. );
  1642. }
  1643. #[test]
  1644. fn test_parse_section_multiple_maps() {
  1645. let mut obj = fake_obj();
  1646. fake_sym(&mut obj, 0, 0, "foo", mem::size_of::<bpf_map_def>() as u64);
  1647. fake_sym(&mut obj, 0, 28, "bar", mem::size_of::<bpf_map_def>() as u64);
  1648. fake_sym(&mut obj, 0, 60, "baz", mem::size_of::<bpf_map_def>() as u64);
  1649. let def = &bpf_map_def {
  1650. map_type: 1,
  1651. key_size: 2,
  1652. value_size: 3,
  1653. max_entries: 4,
  1654. map_flags: 5,
  1655. ..Default::default()
  1656. };
  1657. let map_data = bytes_of(def).to_vec();
  1658. let mut buf = vec![];
  1659. buf.extend(&map_data);
  1660. buf.extend(&map_data);
  1661. // throw in some padding
  1662. buf.extend([0, 0, 0, 0]);
  1663. buf.extend(&map_data);
  1664. assert_matches!(
  1665. obj.parse_section(fake_section(
  1666. EbpfSectionKind::Maps,
  1667. "maps",
  1668. buf.as_slice(),
  1669. None
  1670. )),
  1671. Ok(())
  1672. );
  1673. assert!(obj.maps.contains_key("foo"));
  1674. assert!(obj.maps.contains_key("bar"));
  1675. assert!(obj.maps.contains_key("baz"));
  1676. for map in obj.maps.values() {
  1677. assert_matches!(map, Map::Legacy(m) => {
  1678. assert_eq!(&m.def, def);
  1679. })
  1680. }
  1681. }
  1682. #[test]
  1683. fn test_parse_section_data() {
  1684. let mut obj = fake_obj();
  1685. assert_matches!(
  1686. obj.parse_section(fake_section(
  1687. EbpfSectionKind::Data,
  1688. ".bss",
  1689. b"map data",
  1690. None
  1691. )),
  1692. Ok(())
  1693. );
  1694. assert!(obj.maps.contains_key(".bss"));
  1695. assert_matches!(
  1696. obj.parse_section(fake_section(
  1697. EbpfSectionKind::Data,
  1698. ".rodata",
  1699. b"map data",
  1700. None
  1701. )),
  1702. Ok(())
  1703. );
  1704. assert!(obj.maps.contains_key(".rodata"));
  1705. assert_matches!(
  1706. obj.parse_section(fake_section(
  1707. EbpfSectionKind::Data,
  1708. ".rodata.boo",
  1709. b"map data",
  1710. None
  1711. )),
  1712. Ok(())
  1713. );
  1714. assert!(obj.maps.contains_key(".rodata.boo"));
  1715. assert_matches!(
  1716. obj.parse_section(fake_section(
  1717. EbpfSectionKind::Data,
  1718. ".data",
  1719. b"map data",
  1720. None
  1721. )),
  1722. Ok(())
  1723. );
  1724. assert!(obj.maps.contains_key(".data"));
  1725. assert_matches!(
  1726. obj.parse_section(fake_section(
  1727. EbpfSectionKind::Data,
  1728. ".data.boo",
  1729. b"map data",
  1730. None
  1731. )),
  1732. Ok(())
  1733. );
  1734. assert!(obj.maps.contains_key(".data.boo"));
  1735. }
  1736. #[test]
  1737. fn test_parse_section_kprobe() {
  1738. let mut obj = fake_obj();
  1739. fake_sym(&mut obj, 0, 0, "foo", FAKE_INS_LEN);
  1740. assert_matches!(
  1741. obj.parse_section(fake_section(
  1742. EbpfSectionKind::Program,
  1743. "kprobe/foo",
  1744. bytes_of(&fake_ins()),
  1745. None
  1746. )),
  1747. Ok(())
  1748. );
  1749. assert_matches!(
  1750. obj.programs.get("foo"),
  1751. Some(Program {
  1752. section: ProgramSection::KProbe,
  1753. ..
  1754. })
  1755. );
  1756. }
  1757. #[test]
  1758. fn test_parse_section_uprobe() {
  1759. let mut obj = fake_obj();
  1760. fake_sym(&mut obj, 0, 0, "foo", FAKE_INS_LEN);
  1761. assert_matches!(
  1762. obj.parse_section(fake_section(
  1763. EbpfSectionKind::Program,
  1764. "uprobe/foo",
  1765. bytes_of(&fake_ins()),
  1766. None
  1767. )),
  1768. Ok(())
  1769. );
  1770. assert_matches!(
  1771. obj.programs.get("foo"),
  1772. Some(Program {
  1773. section: ProgramSection::UProbe { .. },
  1774. ..
  1775. })
  1776. );
  1777. }
  1778. #[test]
  1779. fn test_parse_section_uprobe_sleepable() {
  1780. let mut obj = fake_obj();
  1781. fake_sym(&mut obj, 0, 0, "foo", FAKE_INS_LEN);
  1782. assert_matches!(
  1783. obj.parse_section(fake_section(
  1784. EbpfSectionKind::Program,
  1785. "uprobe.s/foo",
  1786. bytes_of(&fake_ins()),
  1787. None
  1788. )),
  1789. Ok(())
  1790. );
  1791. assert_matches!(
  1792. obj.programs.get("foo"),
  1793. Some(Program {
  1794. section: ProgramSection::UProbe {
  1795. sleepable: true,
  1796. ..
  1797. },
  1798. ..
  1799. })
  1800. );
  1801. }
  1802. #[test]
  1803. fn test_parse_section_uretprobe() {
  1804. let mut obj = fake_obj();
  1805. fake_sym(&mut obj, 0, 0, "foo", FAKE_INS_LEN);
  1806. assert_matches!(
  1807. obj.parse_section(fake_section(
  1808. EbpfSectionKind::Program,
  1809. "uretprobe/foo",
  1810. bytes_of(&fake_ins()),
  1811. None
  1812. )),
  1813. Ok(())
  1814. );
  1815. assert_matches!(
  1816. obj.programs.get("foo"),
  1817. Some(Program {
  1818. section: ProgramSection::URetProbe { .. },
  1819. ..
  1820. })
  1821. );
  1822. }
  1823. #[test]
  1824. fn test_parse_section_uretprobe_sleepable() {
  1825. let mut obj = fake_obj();
  1826. fake_sym(&mut obj, 0, 0, "foo", FAKE_INS_LEN);
  1827. assert_matches!(
  1828. obj.parse_section(fake_section(
  1829. EbpfSectionKind::Program,
  1830. "uretprobe.s/foo",
  1831. bytes_of(&fake_ins()),
  1832. None
  1833. )),
  1834. Ok(())
  1835. );
  1836. assert_matches!(
  1837. obj.programs.get("foo"),
  1838. Some(Program {
  1839. section: ProgramSection::URetProbe {
  1840. sleepable: true,
  1841. ..
  1842. },
  1843. ..
  1844. })
  1845. );
  1846. }
  1847. #[test]
  1848. fn test_parse_section_trace_point() {
  1849. let mut obj = fake_obj();
  1850. fake_sym(&mut obj, 0, 0, "foo", FAKE_INS_LEN);
  1851. fake_sym(&mut obj, 1, 0, "bar", FAKE_INS_LEN);
  1852. assert_matches!(
  1853. obj.parse_section(fake_section(
  1854. EbpfSectionKind::Program,
  1855. "tracepoint/foo",
  1856. bytes_of(&fake_ins()),
  1857. None
  1858. )),
  1859. Ok(())
  1860. );
  1861. assert_matches!(
  1862. obj.programs.get("foo"),
  1863. Some(Program {
  1864. section: ProgramSection::TracePoint,
  1865. ..
  1866. })
  1867. );
  1868. assert_matches!(
  1869. obj.parse_section(fake_section(
  1870. EbpfSectionKind::Program,
  1871. "tp/foo/bar",
  1872. bytes_of(&fake_ins()),
  1873. Some(1),
  1874. )),
  1875. Ok(())
  1876. );
  1877. assert_matches!(
  1878. obj.programs.get("bar"),
  1879. Some(Program {
  1880. section: ProgramSection::TracePoint,
  1881. ..
  1882. })
  1883. );
  1884. }
  1885. #[test]
  1886. fn test_parse_section_socket_filter() {
  1887. let mut obj = fake_obj();
  1888. fake_sym(&mut obj, 0, 0, "foo", FAKE_INS_LEN);
  1889. assert_matches!(
  1890. obj.parse_section(fake_section(
  1891. EbpfSectionKind::Program,
  1892. "socket/foo",
  1893. bytes_of(&fake_ins()),
  1894. None
  1895. )),
  1896. Ok(())
  1897. );
  1898. assert_matches!(
  1899. obj.programs.get("foo"),
  1900. Some(Program {
  1901. section: ProgramSection::SocketFilter,
  1902. ..
  1903. })
  1904. );
  1905. }
  1906. #[test]
  1907. fn test_parse_section_xdp() {
  1908. let mut obj = fake_obj();
  1909. fake_sym(&mut obj, 0, 0, "foo", FAKE_INS_LEN);
  1910. assert_matches!(
  1911. obj.parse_section(fake_section(
  1912. EbpfSectionKind::Program,
  1913. "xdp",
  1914. bytes_of(&fake_ins()),
  1915. None
  1916. )),
  1917. Ok(())
  1918. );
  1919. assert_matches!(
  1920. obj.programs.get("foo"),
  1921. Some(Program {
  1922. section: ProgramSection::Xdp { frags: false, .. },
  1923. ..
  1924. })
  1925. );
  1926. }
  1927. #[test]
  1928. fn test_parse_section_xdp_frags() {
  1929. let mut obj = fake_obj();
  1930. fake_sym(&mut obj, 0, 0, "foo", FAKE_INS_LEN);
  1931. assert_matches!(
  1932. obj.parse_section(fake_section(
  1933. EbpfSectionKind::Program,
  1934. "xdp.frags",
  1935. bytes_of(&fake_ins()),
  1936. None
  1937. )),
  1938. Ok(())
  1939. );
  1940. assert_matches!(
  1941. obj.programs.get("foo"),
  1942. Some(Program {
  1943. section: ProgramSection::Xdp { frags: true, .. },
  1944. ..
  1945. })
  1946. );
  1947. }
  1948. #[test]
  1949. fn test_parse_section_raw_tp() {
  1950. let mut obj = fake_obj();
  1951. fake_sym(&mut obj, 0, 0, "foo", FAKE_INS_LEN);
  1952. fake_sym(&mut obj, 1, 0, "bar", FAKE_INS_LEN);
  1953. assert_matches!(
  1954. obj.parse_section(fake_section(
  1955. EbpfSectionKind::Program,
  1956. "raw_tp/foo",
  1957. bytes_of(&fake_ins()),
  1958. None
  1959. )),
  1960. Ok(())
  1961. );
  1962. assert_matches!(
  1963. obj.programs.get("foo"),
  1964. Some(Program {
  1965. section: ProgramSection::RawTracePoint,
  1966. ..
  1967. })
  1968. );
  1969. assert_matches!(
  1970. obj.parse_section(fake_section(
  1971. EbpfSectionKind::Program,
  1972. "raw_tracepoint/bar",
  1973. bytes_of(&fake_ins()),
  1974. Some(1)
  1975. )),
  1976. Ok(())
  1977. );
  1978. assert_matches!(
  1979. obj.programs.get("bar"),
  1980. Some(Program {
  1981. section: ProgramSection::RawTracePoint,
  1982. ..
  1983. })
  1984. );
  1985. }
  1986. #[test]
  1987. fn test_parse_section_lsm() {
  1988. let mut obj = fake_obj();
  1989. fake_sym(&mut obj, 0, 0, "foo", FAKE_INS_LEN);
  1990. assert_matches!(
  1991. obj.parse_section(fake_section(
  1992. EbpfSectionKind::Program,
  1993. "lsm/foo",
  1994. bytes_of(&fake_ins()),
  1995. None
  1996. )),
  1997. Ok(())
  1998. );
  1999. assert_matches!(
  2000. obj.programs.get("foo"),
  2001. Some(Program {
  2002. section: ProgramSection::Lsm {
  2003. sleepable: false,
  2004. ..
  2005. },
  2006. ..
  2007. })
  2008. );
  2009. }
  2010. #[test]
  2011. fn test_parse_section_lsm_sleepable() {
  2012. let mut obj = fake_obj();
  2013. fake_sym(&mut obj, 0, 0, "foo", FAKE_INS_LEN);
  2014. assert_matches!(
  2015. obj.parse_section(fake_section(
  2016. EbpfSectionKind::Program,
  2017. "lsm.s/foo",
  2018. bytes_of(&fake_ins()),
  2019. None
  2020. )),
  2021. Ok(())
  2022. );
  2023. assert_matches!(
  2024. obj.programs.get("foo"),
  2025. Some(Program {
  2026. section: ProgramSection::Lsm {
  2027. sleepable: true,
  2028. ..
  2029. },
  2030. ..
  2031. })
  2032. );
  2033. }
  2034. #[test]
  2035. fn test_parse_section_btf_tracepoint() {
  2036. let mut obj = fake_obj();
  2037. fake_sym(&mut obj, 0, 0, "foo", FAKE_INS_LEN);
  2038. assert_matches!(
  2039. obj.parse_section(fake_section(
  2040. EbpfSectionKind::Program,
  2041. "tp_btf/foo",
  2042. bytes_of(&fake_ins()),
  2043. None
  2044. )),
  2045. Ok(())
  2046. );
  2047. assert_matches!(
  2048. obj.programs.get("foo"),
  2049. Some(Program {
  2050. section: ProgramSection::BtfTracePoint,
  2051. ..
  2052. })
  2053. );
  2054. }
  2055. #[test]
  2056. fn test_parse_section_skskb_unnamed() {
  2057. let mut obj = fake_obj();
  2058. fake_sym(&mut obj, 0, 0, "stream_parser", FAKE_INS_LEN);
  2059. assert_matches!(
  2060. obj.parse_section(fake_section(
  2061. EbpfSectionKind::Program,
  2062. "sk_skb/stream_parser",
  2063. bytes_of(&fake_ins()),
  2064. None
  2065. )),
  2066. Ok(())
  2067. );
  2068. assert_matches!(
  2069. obj.programs.get("stream_parser"),
  2070. Some(Program {
  2071. section: ProgramSection::SkSkbStreamParser,
  2072. ..
  2073. })
  2074. );
  2075. }
  2076. #[test]
  2077. fn test_parse_section_skskb_named() {
  2078. let mut obj = fake_obj();
  2079. fake_sym(&mut obj, 0, 0, "my_parser", FAKE_INS_LEN);
  2080. assert_matches!(
  2081. obj.parse_section(fake_section(
  2082. EbpfSectionKind::Program,
  2083. "sk_skb/stream_parser/my_parser",
  2084. bytes_of(&fake_ins()),
  2085. None
  2086. )),
  2087. Ok(())
  2088. );
  2089. assert_matches!(
  2090. obj.programs.get("my_parser"),
  2091. Some(Program {
  2092. section: ProgramSection::SkSkbStreamParser,
  2093. ..
  2094. })
  2095. );
  2096. }
  2097. #[test]
  2098. fn test_parse_section_fentry() {
  2099. let mut obj = fake_obj();
  2100. fake_sym(&mut obj, 0, 0, "foo", FAKE_INS_LEN);
  2101. assert_matches!(
  2102. obj.parse_section(fake_section(
  2103. EbpfSectionKind::Program,
  2104. "fentry/foo",
  2105. bytes_of(&fake_ins()),
  2106. None
  2107. )),
  2108. Ok(())
  2109. );
  2110. assert_matches!(
  2111. obj.programs.get("foo"),
  2112. Some(Program {
  2113. section: ProgramSection::FEntry { .. },
  2114. ..
  2115. })
  2116. );
  2117. }
  2118. #[test]
  2119. fn test_parse_section_fentry_sleepable() {
  2120. let mut obj = fake_obj();
  2121. fake_sym(&mut obj, 0, 0, "foo", FAKE_INS_LEN);
  2122. assert_matches!(
  2123. obj.parse_section(fake_section(
  2124. EbpfSectionKind::Program,
  2125. "fentry.s/foo",
  2126. bytes_of(&fake_ins()),
  2127. None
  2128. )),
  2129. Ok(())
  2130. );
  2131. assert_matches!(
  2132. obj.programs.get("foo"),
  2133. Some(Program {
  2134. section: ProgramSection::FEntry {
  2135. sleepable: true,
  2136. ..
  2137. },
  2138. ..
  2139. })
  2140. );
  2141. }
  2142. #[test]
  2143. fn test_parse_section_fexit() {
  2144. let mut obj = fake_obj();
  2145. fake_sym(&mut obj, 0, 0, "foo", FAKE_INS_LEN);
  2146. assert_matches!(
  2147. obj.parse_section(fake_section(
  2148. EbpfSectionKind::Program,
  2149. "fexit/foo",
  2150. bytes_of(&fake_ins()),
  2151. None
  2152. )),
  2153. Ok(())
  2154. );
  2155. assert_matches!(
  2156. obj.programs.get("foo"),
  2157. Some(Program {
  2158. section: ProgramSection::FExit { .. },
  2159. ..
  2160. })
  2161. );
  2162. }
  2163. #[test]
  2164. fn test_parse_section_fexit_sleepable() {
  2165. let mut obj = fake_obj();
  2166. fake_sym(&mut obj, 0, 0, "foo", FAKE_INS_LEN);
  2167. assert_matches!(
  2168. obj.parse_section(fake_section(
  2169. EbpfSectionKind::Program,
  2170. "fexit.s/foo",
  2171. bytes_of(&fake_ins()),
  2172. None
  2173. )),
  2174. Ok(())
  2175. );
  2176. assert_matches!(
  2177. obj.programs.get("foo"),
  2178. Some(Program {
  2179. section: ProgramSection::FExit {
  2180. sleepable: true,
  2181. ..
  2182. },
  2183. ..
  2184. })
  2185. );
  2186. }
  2187. #[test]
  2188. fn test_parse_section_cgroup_skb_ingress_unnamed() {
  2189. let mut obj = fake_obj();
  2190. fake_sym(&mut obj, 0, 0, "ingress", FAKE_INS_LEN);
  2191. assert_matches!(
  2192. obj.parse_section(fake_section(
  2193. EbpfSectionKind::Program,
  2194. "cgroup_skb/ingress",
  2195. bytes_of(&fake_ins()),
  2196. None
  2197. )),
  2198. Ok(())
  2199. );
  2200. assert_matches!(
  2201. obj.programs.get("ingress"),
  2202. Some(Program {
  2203. section: ProgramSection::CgroupSkbIngress,
  2204. ..
  2205. })
  2206. );
  2207. }
  2208. #[test]
  2209. fn test_parse_section_cgroup_skb_ingress_named() {
  2210. let mut obj = fake_obj();
  2211. fake_sym(&mut obj, 0, 0, "foo", FAKE_INS_LEN);
  2212. assert_matches!(
  2213. obj.parse_section(fake_section(
  2214. EbpfSectionKind::Program,
  2215. "cgroup_skb/ingress/foo",
  2216. bytes_of(&fake_ins()),
  2217. None
  2218. )),
  2219. Ok(())
  2220. );
  2221. assert_matches!(
  2222. obj.programs.get("foo"),
  2223. Some(Program {
  2224. section: ProgramSection::CgroupSkbIngress,
  2225. ..
  2226. })
  2227. );
  2228. }
  2229. #[test]
  2230. fn test_parse_section_cgroup_skb_no_direction_unamed() {
  2231. let mut obj = fake_obj();
  2232. fake_sym(&mut obj, 0, 0, "skb", FAKE_INS_LEN);
  2233. assert_matches!(
  2234. obj.parse_section(fake_section(
  2235. EbpfSectionKind::Program,
  2236. "cgroup/skb",
  2237. bytes_of(&fake_ins()),
  2238. None
  2239. )),
  2240. Ok(())
  2241. );
  2242. assert_matches!(
  2243. obj.programs.get("skb"),
  2244. Some(Program {
  2245. section: ProgramSection::CgroupSkb,
  2246. ..
  2247. })
  2248. );
  2249. }
  2250. #[test]
  2251. fn test_parse_section_cgroup_skb_no_direction_named() {
  2252. let mut obj = fake_obj();
  2253. fake_sym(&mut obj, 0, 0, "foo", FAKE_INS_LEN);
  2254. assert_matches!(
  2255. obj.parse_section(fake_section(
  2256. EbpfSectionKind::Program,
  2257. "cgroup/skb/foo",
  2258. bytes_of(&fake_ins()),
  2259. None
  2260. )),
  2261. Ok(())
  2262. );
  2263. assert_matches!(
  2264. obj.programs.get("foo"),
  2265. Some(Program {
  2266. section: ProgramSection::CgroupSkb,
  2267. ..
  2268. })
  2269. );
  2270. }
  2271. #[test]
  2272. fn test_parse_section_sock_addr_named() {
  2273. let mut obj = fake_obj();
  2274. fake_sym(&mut obj, 0, 0, "foo", FAKE_INS_LEN);
  2275. assert_matches!(
  2276. obj.parse_section(fake_section(
  2277. EbpfSectionKind::Program,
  2278. "cgroup/connect4/foo",
  2279. bytes_of(&fake_ins()),
  2280. None
  2281. )),
  2282. Ok(())
  2283. );
  2284. assert_matches!(
  2285. obj.programs.get("foo"),
  2286. Some(Program {
  2287. section: ProgramSection::CgroupSockAddr {
  2288. attach_type: CgroupSockAddrAttachType::Connect4,
  2289. ..
  2290. },
  2291. ..
  2292. })
  2293. );
  2294. }
  2295. #[test]
  2296. fn test_parse_section_sock_addr_unnamed() {
  2297. let mut obj = fake_obj();
  2298. fake_sym(&mut obj, 0, 0, "connect4", FAKE_INS_LEN);
  2299. assert_matches!(
  2300. obj.parse_section(fake_section(
  2301. EbpfSectionKind::Program,
  2302. "cgroup/connect4",
  2303. bytes_of(&fake_ins()),
  2304. None
  2305. )),
  2306. Ok(())
  2307. );
  2308. assert_matches!(
  2309. obj.programs.get("connect4"),
  2310. Some(Program {
  2311. section: ProgramSection::CgroupSockAddr {
  2312. attach_type: CgroupSockAddrAttachType::Connect4,
  2313. ..
  2314. },
  2315. ..
  2316. })
  2317. );
  2318. }
  2319. #[test]
  2320. fn test_parse_section_sockopt_named() {
  2321. let mut obj = fake_obj();
  2322. fake_sym(&mut obj, 0, 0, "foo", FAKE_INS_LEN);
  2323. assert_matches!(
  2324. obj.parse_section(fake_section(
  2325. EbpfSectionKind::Program,
  2326. "cgroup/getsockopt/foo",
  2327. bytes_of(&fake_ins()),
  2328. None
  2329. )),
  2330. Ok(())
  2331. );
  2332. assert_matches!(
  2333. obj.programs.get("foo"),
  2334. Some(Program {
  2335. section: ProgramSection::CgroupSockopt {
  2336. attach_type: CgroupSockoptAttachType::Get,
  2337. ..
  2338. },
  2339. ..
  2340. })
  2341. );
  2342. }
  2343. #[test]
  2344. fn test_parse_section_sockopt_unnamed() {
  2345. let mut obj = fake_obj();
  2346. fake_sym(&mut obj, 0, 0, "getsockopt", FAKE_INS_LEN);
  2347. assert_matches!(
  2348. obj.parse_section(fake_section(
  2349. EbpfSectionKind::Program,
  2350. "cgroup/getsockopt",
  2351. bytes_of(&fake_ins()),
  2352. None
  2353. )),
  2354. Ok(())
  2355. );
  2356. assert_matches!(
  2357. obj.programs.get("getsockopt"),
  2358. Some(Program {
  2359. section: ProgramSection::CgroupSockopt {
  2360. attach_type: CgroupSockoptAttachType::Get,
  2361. ..
  2362. },
  2363. ..
  2364. })
  2365. );
  2366. }
  2367. #[test]
  2368. fn test_patch_map_data() {
  2369. let mut obj = fake_obj();
  2370. obj.maps.insert(
  2371. ".rodata".to_owned(),
  2372. Map::Legacy(LegacyMap {
  2373. def: bpf_map_def {
  2374. map_type: BPF_MAP_TYPE_ARRAY as u32,
  2375. key_size: mem::size_of::<u32>() as u32,
  2376. value_size: 3,
  2377. max_entries: 1,
  2378. map_flags: BPF_F_RDONLY_PROG,
  2379. id: 1,
  2380. pinning: PinningType::None,
  2381. },
  2382. section_index: 1,
  2383. section_kind: EbpfSectionKind::Rodata,
  2384. symbol_index: Some(1),
  2385. data: vec![0, 0, 0],
  2386. }),
  2387. );
  2388. obj.symbol_table.insert(
  2389. 1,
  2390. Symbol {
  2391. index: 1,
  2392. section_index: Some(1),
  2393. name: Some("my_config".to_owned()),
  2394. address: 0,
  2395. size: 3,
  2396. is_definition: true,
  2397. kind: SymbolKind::Data,
  2398. },
  2399. );
  2400. let test_data: &[u8] = &[1, 2, 3];
  2401. obj.patch_map_data(HashMap::from([
  2402. ("my_config", (test_data, true)),
  2403. ("optional_variable", (test_data, false)),
  2404. ]))
  2405. .unwrap();
  2406. let map = obj.maps.get(".rodata").unwrap();
  2407. assert_eq!(test_data, map.data());
  2408. }
  2409. #[test]
  2410. fn test_parse_btf_map_section() {
  2411. let mut obj = fake_obj();
  2412. fake_sym(&mut obj, 0, 0, "map_1", 0);
  2413. fake_sym(&mut obj, 0, 0, "map_2", 0);
  2414. // generated from:
  2415. // objcopy --dump-section .BTF=test.btf ./target/bpfel-unknown-none/debug/multimap-btf.bpf.o
  2416. // hexdump -v -e '7/1 "0x%02X, " 1/1 " 0x%02X,\n"' test.btf
  2417. let data: &[u8] = if cfg!(target_endian = "little") {
  2418. &[
  2419. 0x9F, 0xEB, 0x01, 0x00, 0x18, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF0, 0x01,
  2420. 0x00, 0x00, 0xF0, 0x01, 0x00, 0x00, 0xCC, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  2421. 0x00, 0x00, 0x00, 0x02, 0x03, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
  2422. 0x00, 0x01, 0x04, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00,
  2423. 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x04, 0x00,
  2424. 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
  2425. 0x04, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  2426. 0x00, 0x02, 0x06, 0x00, 0x00, 0x00, 0x19, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08,
  2427. 0x07, 0x00, 0x00, 0x00, 0x1F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x04, 0x00,
  2428. 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02,
  2429. 0x09, 0x00, 0x00, 0x00, 0x2C, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x0A, 0x00,
  2430. 0x00, 0x00, 0x32, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x08, 0x00, 0x00, 0x00,
  2431. 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x0C, 0x00,
  2432. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00,
  2433. 0x02, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
  2434. 0x00, 0x00, 0x04, 0x00, 0x00, 0x04, 0x20, 0x00, 0x00, 0x00, 0x45, 0x00, 0x00, 0x00,
  2435. 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x4A, 0x00, 0x00, 0x00, 0x05, 0x00,
  2436. 0x00, 0x00, 0x40, 0x00, 0x00, 0x00, 0x4E, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00,
  2437. 0x80, 0x00, 0x00, 0x00, 0x54, 0x00, 0x00, 0x00, 0x0B, 0x00, 0x00, 0x00, 0xC0, 0x00,
  2438. 0x00, 0x00, 0x60, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0E, 0x0D, 0x00, 0x00, 0x00,
  2439. 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x04, 0x20, 0x00,
  2440. 0x00, 0x00, 0x45, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  2441. 0x4A, 0x00, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00, 0x4E, 0x00,
  2442. 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00, 0x54, 0x00, 0x00, 0x00,
  2443. 0x0B, 0x00, 0x00, 0x00, 0xC0, 0x00, 0x00, 0x00, 0x66, 0x00, 0x00, 0x00, 0x00, 0x00,
  2444. 0x00, 0x0E, 0x0F, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  2445. 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00,
  2446. 0x00, 0x0D, 0x02, 0x00, 0x00, 0x00, 0x6C, 0x00, 0x00, 0x00, 0x11, 0x00, 0x00, 0x00,
  2447. 0x70, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x0C, 0x12, 0x00, 0x00, 0x00, 0xB0, 0x01,
  2448. 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x01,
  2449. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x14, 0x00,
  2450. 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0xB5, 0x01, 0x00, 0x00,
  2451. 0x00, 0x00, 0x00, 0x0E, 0x15, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0xBE, 0x01,
  2452. 0x00, 0x00, 0x02, 0x00, 0x00, 0x0F, 0x00, 0x00, 0x00, 0x00, 0x0E, 0x00, 0x00, 0x00,
  2453. 0x00, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00,
  2454. 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0xC4, 0x01, 0x00, 0x00, 0x01, 0x00, 0x00, 0x0F,
  2455. 0x00, 0x00, 0x00, 0x00, 0x16, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00,
  2456. 0x00, 0x00, 0x00, 0x69, 0x6E, 0x74, 0x00, 0x5F, 0x5F, 0x41, 0x52, 0x52, 0x41, 0x59,
  2457. 0x5F, 0x53, 0x49, 0x5A, 0x45, 0x5F, 0x54, 0x59, 0x50, 0x45, 0x5F, 0x5F, 0x00, 0x5F,
  2458. 0x5F, 0x75, 0x33, 0x32, 0x00, 0x75, 0x6E, 0x73, 0x69, 0x67, 0x6E, 0x65, 0x64, 0x20,
  2459. 0x69, 0x6E, 0x74, 0x00, 0x5F, 0x5F, 0x75, 0x36, 0x34, 0x00, 0x75, 0x6E, 0x73, 0x69,
  2460. 0x67, 0x6E, 0x65, 0x64, 0x20, 0x6C, 0x6F, 0x6E, 0x67, 0x20, 0x6C, 0x6F, 0x6E, 0x67,
  2461. 0x00, 0x74, 0x79, 0x70, 0x65, 0x00, 0x6B, 0x65, 0x79, 0x00, 0x76, 0x61, 0x6C, 0x75,
  2462. 0x65, 0x00, 0x6D, 0x61, 0x78, 0x5F, 0x65, 0x6E, 0x74, 0x72, 0x69, 0x65, 0x73, 0x00,
  2463. 0x6D, 0x61, 0x70, 0x5F, 0x31, 0x00, 0x6D, 0x61, 0x70, 0x5F, 0x32, 0x00, 0x63, 0x74,
  2464. 0x78, 0x00, 0x62, 0x70, 0x66, 0x5F, 0x70, 0x72, 0x6F, 0x67, 0x00, 0x74, 0x72, 0x61,
  2465. 0x63, 0x65, 0x70, 0x6F, 0x69, 0x6E, 0x74, 0x00, 0x2F, 0x76, 0x61, 0x72, 0x2F, 0x68,
  2466. 0x6F, 0x6D, 0x65, 0x2F, 0x64, 0x61, 0x76, 0x65, 0x2F, 0x64, 0x65, 0x76, 0x2F, 0x61,
  2467. 0x79, 0x61, 0x2D, 0x72, 0x73, 0x2F, 0x61, 0x79, 0x61, 0x2F, 0x74, 0x65, 0x73, 0x74,
  2468. 0x2F, 0x69, 0x6E, 0x74, 0x65, 0x67, 0x72, 0x61, 0x74, 0x69, 0x6F, 0x6E, 0x2D, 0x65,
  2469. 0x62, 0x70, 0x66, 0x2F, 0x73, 0x72, 0x63, 0x2F, 0x62, 0x70, 0x66, 0x2F, 0x6D, 0x75,
  2470. 0x6C, 0x74, 0x69, 0x6D, 0x61, 0x70, 0x2D, 0x62, 0x74, 0x66, 0x2E, 0x62, 0x70, 0x66,
  2471. 0x2E, 0x63, 0x00, 0x69, 0x6E, 0x74, 0x20, 0x62, 0x70, 0x66, 0x5F, 0x70, 0x72, 0x6F,
  2472. 0x67, 0x28, 0x76, 0x6F, 0x69, 0x64, 0x20, 0x2A, 0x63, 0x74, 0x78, 0x29, 0x00, 0x09,
  2473. 0x5F, 0x5F, 0x75, 0x33, 0x32, 0x20, 0x6B, 0x65, 0x79, 0x20, 0x3D, 0x20, 0x30, 0x3B,
  2474. 0x00, 0x09, 0x5F, 0x5F, 0x75, 0x36, 0x34, 0x20, 0x74, 0x77, 0x65, 0x6E, 0x74, 0x79,
  2475. 0x5F, 0x66, 0x6F, 0x75, 0x72, 0x20, 0x3D, 0x20, 0x32, 0x34, 0x3B, 0x00, 0x09, 0x5F,
  2476. 0x5F, 0x75, 0x36, 0x34, 0x20, 0x66, 0x6F, 0x72, 0x74, 0x79, 0x5F, 0x74, 0x77, 0x6F,
  2477. 0x20, 0x3D, 0x20, 0x34, 0x32, 0x3B, 0x00, 0x20, 0x20, 0x20, 0x20, 0x62, 0x70, 0x66,
  2478. 0x5F, 0x6D, 0x61, 0x70, 0x5F, 0x75, 0x70, 0x64, 0x61, 0x74, 0x65, 0x5F, 0x65, 0x6C,
  2479. 0x65, 0x6D, 0x28, 0x26, 0x6D, 0x61, 0x70, 0x5F, 0x31, 0x2C, 0x20, 0x26, 0x6B, 0x65,
  2480. 0x79, 0x2C, 0x20, 0x26, 0x74, 0x77, 0x65, 0x6E, 0x74, 0x79, 0x5F, 0x66, 0x6F, 0x75,
  2481. 0x72, 0x2C, 0x20, 0x42, 0x50, 0x46, 0x5F, 0x41, 0x4E, 0x59, 0x29, 0x3B, 0x00, 0x20,
  2482. 0x20, 0x20, 0x20, 0x62, 0x70, 0x66, 0x5F, 0x6D, 0x61, 0x70, 0x5F, 0x75, 0x70, 0x64,
  2483. 0x61, 0x74, 0x65, 0x5F, 0x65, 0x6C, 0x65, 0x6D, 0x28, 0x26, 0x6D, 0x61, 0x70, 0x5F,
  2484. 0x32, 0x2C, 0x20, 0x26, 0x6B, 0x65, 0x79, 0x2C, 0x20, 0x26, 0x66, 0x6F, 0x72, 0x74,
  2485. 0x79, 0x5F, 0x74, 0x77, 0x6F, 0x2C, 0x20, 0x42, 0x50, 0x46, 0x5F, 0x41, 0x4E, 0x59,
  2486. 0x29, 0x3B, 0x00, 0x09, 0x72, 0x65, 0x74, 0x75, 0x72, 0x6E, 0x20, 0x30, 0x3B, 0x00,
  2487. 0x63, 0x68, 0x61, 0x72, 0x00, 0x5F, 0x6C, 0x69, 0x63, 0x65, 0x6E, 0x73, 0x65, 0x00,
  2488. 0x2E, 0x6D, 0x61, 0x70, 0x73, 0x00, 0x6C, 0x69, 0x63, 0x65, 0x6E, 0x73, 0x65, 0x00,
  2489. ]
  2490. } else {
  2491. &[
  2492. 0xEB, 0x9F, 0x01, 0x00, 0x00, 0x00, 0x00, 0x18, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  2493. 0x01, 0xF0, 0x00, 0x00, 0x01, 0xF0, 0x00, 0x00, 0x01, 0xCC, 0x00, 0x00, 0x00, 0x00,
  2494. 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x01, 0x01, 0x00,
  2495. 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x01, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00,
  2496. 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00,
  2497. 0x00, 0x04, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x05, 0x01, 0x00, 0x00, 0x00,
  2498. 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00,
  2499. 0x00, 0x00, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x19, 0x08, 0x00, 0x00, 0x00,
  2500. 0x00, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00, 0x1F, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
  2501. 0x00, 0x04, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
  2502. 0x00, 0x00, 0x00, 0x09, 0x00, 0x00, 0x00, 0x2C, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00,
  2503. 0x00, 0x0A, 0x00, 0x00, 0x00, 0x32, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08,
  2504. 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00,
  2505. 0x00, 0x0C, 0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  2506. 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
  2507. 0x00, 0x00, 0x04, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x45,
  2508. 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x4A, 0x00, 0x00,
  2509. 0x00, 0x05, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00, 0x4E, 0x00, 0x00, 0x00, 0x08,
  2510. 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00, 0x54, 0x00, 0x00, 0x00, 0x0B, 0x00, 0x00,
  2511. 0x00, 0xC0, 0x00, 0x00, 0x00, 0x60, 0x0E, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0D,
  2512. 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x04, 0x00, 0x00,
  2513. 0x00, 0x20, 0x00, 0x00, 0x00, 0x45, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00,
  2514. 0x00, 0x00, 0x00, 0x4A, 0x00, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00,
  2515. 0x00, 0x4E, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00, 0x54,
  2516. 0x00, 0x00, 0x00, 0x0B, 0x00, 0x00, 0x00, 0xC0, 0x00, 0x00, 0x00, 0x66, 0x0E, 0x00,
  2517. 0x00, 0x00, 0x00, 0x00, 0x00, 0x0F, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00,
  2518. 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0D, 0x00,
  2519. 0x00, 0x01, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x6C, 0x00, 0x00, 0x00, 0x11,
  2520. 0x00, 0x00, 0x00, 0x70, 0x0C, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x12, 0x00, 0x00,
  2521. 0x01, 0xB0, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x00, 0x00, 0x08,
  2522. 0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  2523. 0x00, 0x14, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x01, 0xB5,
  2524. 0x0E, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x15, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
  2525. 0x01, 0xBE, 0x0F, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0E,
  2526. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00,
  2527. 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x01, 0xC4, 0x0F, 0x00, 0x00, 0x01,
  2528. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x16, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  2529. 0x00, 0x04, 0x00, 0x69, 0x6E, 0x74, 0x00, 0x5F, 0x5F, 0x41, 0x52, 0x52, 0x41, 0x59,
  2530. 0x5F, 0x53, 0x49, 0x5A, 0x45, 0x5F, 0x54, 0x59, 0x50, 0x45, 0x5F, 0x5F, 0x00, 0x5F,
  2531. 0x5F, 0x75, 0x33, 0x32, 0x00, 0x75, 0x6E, 0x73, 0x69, 0x67, 0x6E, 0x65, 0x64, 0x20,
  2532. 0x69, 0x6E, 0x74, 0x00, 0x5F, 0x5F, 0x75, 0x36, 0x34, 0x00, 0x75, 0x6E, 0x73, 0x69,
  2533. 0x67, 0x6E, 0x65, 0x64, 0x20, 0x6C, 0x6F, 0x6E, 0x67, 0x20, 0x6C, 0x6F, 0x6E, 0x67,
  2534. 0x00, 0x74, 0x79, 0x70, 0x65, 0x00, 0x6B, 0x65, 0x79, 0x00, 0x76, 0x61, 0x6C, 0x75,
  2535. 0x65, 0x00, 0x6D, 0x61, 0x78, 0x5F, 0x65, 0x6E, 0x74, 0x72, 0x69, 0x65, 0x73, 0x00,
  2536. 0x6D, 0x61, 0x70, 0x5F, 0x31, 0x00, 0x6D, 0x61, 0x70, 0x5F, 0x32, 0x00, 0x63, 0x74,
  2537. 0x78, 0x00, 0x62, 0x70, 0x66, 0x5F, 0x70, 0x72, 0x6F, 0x67, 0x00, 0x74, 0x72, 0x61,
  2538. 0x63, 0x65, 0x70, 0x6F, 0x69, 0x6E, 0x74, 0x00, 0x2F, 0x76, 0x61, 0x72, 0x2F, 0x68,
  2539. 0x6F, 0x6D, 0x65, 0x2F, 0x64, 0x61, 0x76, 0x65, 0x2F, 0x64, 0x65, 0x76, 0x2F, 0x61,
  2540. 0x79, 0x61, 0x2D, 0x72, 0x73, 0x2F, 0x61, 0x79, 0x61, 0x2F, 0x74, 0x65, 0x73, 0x74,
  2541. 0x2F, 0x69, 0x6E, 0x74, 0x65, 0x67, 0x72, 0x61, 0x74, 0x69, 0x6F, 0x6E, 0x2D, 0x65,
  2542. 0x62, 0x70, 0x66, 0x2F, 0x73, 0x72, 0x63, 0x2F, 0x62, 0x70, 0x66, 0x2F, 0x6D, 0x75,
  2543. 0x6C, 0x74, 0x69, 0x6D, 0x61, 0x70, 0x2D, 0x62, 0x74, 0x66, 0x2E, 0x62, 0x70, 0x66,
  2544. 0x2E, 0x63, 0x00, 0x69, 0x6E, 0x74, 0x20, 0x62, 0x70, 0x66, 0x5F, 0x70, 0x72, 0x6F,
  2545. 0x67, 0x28, 0x76, 0x6F, 0x69, 0x64, 0x20, 0x2A, 0x63, 0x74, 0x78, 0x29, 0x00, 0x09,
  2546. 0x5F, 0x5F, 0x75, 0x33, 0x32, 0x20, 0x6B, 0x65, 0x79, 0x20, 0x3D, 0x20, 0x30, 0x3B,
  2547. 0x00, 0x09, 0x5F, 0x5F, 0x75, 0x36, 0x34, 0x20, 0x74, 0x77, 0x65, 0x6E, 0x74, 0x79,
  2548. 0x5F, 0x66, 0x6F, 0x75, 0x72, 0x20, 0x3D, 0x20, 0x32, 0x34, 0x3B, 0x00, 0x09, 0x5F,
  2549. 0x5F, 0x75, 0x36, 0x34, 0x20, 0x66, 0x6F, 0x72, 0x74, 0x79, 0x5F, 0x74, 0x77, 0x6F,
  2550. 0x20, 0x3D, 0x20, 0x34, 0x32, 0x3B, 0x00, 0x20, 0x20, 0x20, 0x20, 0x62, 0x70, 0x66,
  2551. 0x5F, 0x6D, 0x61, 0x70, 0x5F, 0x75, 0x70, 0x64, 0x61, 0x74, 0x65, 0x5F, 0x65, 0x6C,
  2552. 0x65, 0x6D, 0x28, 0x26, 0x6D, 0x61, 0x70, 0x5F, 0x31, 0x2C, 0x20, 0x26, 0x6B, 0x65,
  2553. 0x79, 0x2C, 0x20, 0x26, 0x74, 0x77, 0x65, 0x6E, 0x74, 0x79, 0x5F, 0x66, 0x6F, 0x75,
  2554. 0x72, 0x2C, 0x20, 0x42, 0x50, 0x46, 0x5F, 0x41, 0x4E, 0x59, 0x29, 0x3B, 0x00, 0x20,
  2555. 0x20, 0x20, 0x20, 0x62, 0x70, 0x66, 0x5F, 0x6D, 0x61, 0x70, 0x5F, 0x75, 0x70, 0x64,
  2556. 0x61, 0x74, 0x65, 0x5F, 0x65, 0x6C, 0x65, 0x6D, 0x28, 0x26, 0x6D, 0x61, 0x70, 0x5F,
  2557. 0x32, 0x2C, 0x20, 0x26, 0x6B, 0x65, 0x79, 0x2C, 0x20, 0x26, 0x66, 0x6F, 0x72, 0x74,
  2558. 0x79, 0x5F, 0x74, 0x77, 0x6F, 0x2C, 0x20, 0x42, 0x50, 0x46, 0x5F, 0x41, 0x4E, 0x59,
  2559. 0x29, 0x3B, 0x00, 0x09, 0x72, 0x65, 0x74, 0x75, 0x72, 0x6E, 0x20, 0x30, 0x3B, 0x00,
  2560. 0x63, 0x68, 0x61, 0x72, 0x00, 0x5F, 0x6C, 0x69, 0x63, 0x65, 0x6E, 0x73, 0x65, 0x00,
  2561. 0x2E, 0x6D, 0x61, 0x70, 0x73, 0x00, 0x6C, 0x69, 0x63, 0x65, 0x6E, 0x73, 0x65, 0x00,
  2562. ]
  2563. };
  2564. let btf_section = fake_section(EbpfSectionKind::Btf, ".BTF", data, None);
  2565. obj.parse_section(btf_section).unwrap();
  2566. let map_section = fake_section(EbpfSectionKind::BtfMaps, ".maps", &[], None);
  2567. obj.parse_section(map_section).unwrap();
  2568. let map = obj.maps.get("map_1").unwrap();
  2569. assert_matches!(map, Map::Btf(m) => {
  2570. assert_eq!(m.def.key_size, 4);
  2571. assert_eq!(m.def.value_size, 8);
  2572. assert_eq!(m.def.max_entries, 1);
  2573. });
  2574. }
  2575. }