lib.rs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. //! An eBPF object file parsing library with BTF and relocation support.
  2. //!
  3. //! # Status
  4. //!
  5. //! This crate includes code that started as internal API used by
  6. //! the [aya] crate. It has been split out so that it can be used by
  7. //! other projects that deal with eBPF object files. Unless you're writing
  8. //! low level eBPF plumbing tools, you should not need to use this crate
  9. //! but see the [aya] crate instead.
  10. //!
  11. //! The API as it is today has a few rough edges and is generally not as
  12. //! polished nor stable as the main [aya] crate API. As always,
  13. //! improvements welcome!
  14. //!
  15. //! [aya]: https://github.com/aya-rs/aya
  16. //!
  17. //! # Overview
  18. //!
  19. //! eBPF programs written with [libbpf] or [aya-bpf] are usually compiled
  20. //! into an ELF object file, using various sections to store information
  21. //! about the eBPF programs.
  22. //!
  23. //! `aya-obj` is a library for parsing such eBPF object files, with BTF and
  24. //! relocation support.
  25. //!
  26. //! [libbpf]: https://github.com/libbpf/libbpf
  27. //! [aya-bpf]: https://github.com/aya-rs/aya
  28. //!
  29. //! # Example
  30. //!
  31. //! This example loads a simple eBPF program and runs it with [rbpf].
  32. //!
  33. //! ```no_run
  34. //! use aya_obj::{generated::bpf_insn, Object};
  35. //!
  36. //! // Parse the object file
  37. //! let bytes = std::fs::read("program.o").unwrap();
  38. //! let mut object = Object::parse(&bytes).unwrap();
  39. //! // Relocate the programs
  40. //! #[cfg(feature = "std")]
  41. //! let text_sections = std::collections::HashSet::new();
  42. //! #[cfg(not(feature = "std"))]
  43. //! let text_sections = hashbrown::HashSet::new();
  44. //! object.relocate_calls(&text_sections).unwrap();
  45. //! object.relocate_maps(std::iter::empty(), &text_sections).unwrap();
  46. //!
  47. //! // Run with rbpf
  48. //! let function = object.functions.get(&object.programs["prog_name"].function_key()).unwrap();
  49. //! let instructions = &function.instructions;
  50. //! let data = unsafe {
  51. //! core::slice::from_raw_parts(
  52. //! instructions.as_ptr() as *const u8,
  53. //! instructions.len() * core::mem::size_of::<bpf_insn>(),
  54. //! )
  55. //! };
  56. //! let vm = rbpf::EbpfVmNoData::new(Some(data)).unwrap();
  57. //! let _return = vm.execute_program().unwrap();
  58. //! ```
  59. //!
  60. //! [rbpf]: https://github.com/qmonnet/rbpf
  61. #![no_std]
  62. #![doc(
  63. html_logo_url = "https://aya-rs.dev/assets/images/crabby.svg",
  64. html_favicon_url = "https://aya-rs.dev/assets/images/crabby.svg"
  65. )]
  66. #![cfg_attr(docsrs, feature(doc_cfg))]
  67. #![deny(clippy::all, missing_docs)]
  68. #![allow(clippy::missing_safety_doc, clippy::len_without_is_empty)]
  69. extern crate alloc;
  70. #[cfg(feature = "std")]
  71. extern crate std;
  72. #[cfg(not(feature = "std"))]
  73. mod std {
  74. pub mod error {
  75. pub use core_error::Error;
  76. }
  77. pub use core::*;
  78. pub mod os {
  79. pub mod fd {
  80. pub type RawFd = core::ffi::c_int;
  81. }
  82. }
  83. }
  84. pub mod btf;
  85. pub mod generated;
  86. pub mod links;
  87. pub mod maps;
  88. pub mod obj;
  89. pub mod programs;
  90. pub mod relocation;
  91. mod util;
  92. pub use maps::Map;
  93. pub use obj::*;
  94. /// An error returned from the verifier.
  95. ///
  96. /// Provides a [`Debug`] implementation that doesn't escape newlines.
  97. pub struct VerifierLog(alloc::string::String);
  98. impl VerifierLog {
  99. /// Create a new verifier log.
  100. pub fn new(log: alloc::string::String) -> Self {
  101. Self(log)
  102. }
  103. }
  104. impl std::fmt::Debug for VerifierLog {
  105. fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
  106. let Self(log) = self;
  107. f.write_str(log)
  108. }
  109. }
  110. impl std::fmt::Display for VerifierLog {
  111. fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
  112. <Self as std::fmt::Debug>::fmt(self, f)
  113. }
  114. }