obj.rs 94 KB

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