Skip to content

Commit

Permalink
aya-obj: migrate aya::obj into a separate crate
Browse files Browse the repository at this point in the history
To split the crate into two, several changes were made:
1. Most `pub(crate)` are now `pub` to allow access from Aya;
2. Parts of BpfError are merged into, for example, RelocationError;
3. BTF part of Features is moved into the new crate;
4. `#![deny(missing_docs)]` is removed temporarily;
5. Some other code gets moved into the new crate, mainly:
   - aya::{bpf_map_def, BtfMapDef, PinningType},
   - aya::programs::{CgroupSock*AttachType},

The new crate is currenly allowing missing_docs. Member visibility
will be adjusted later to minimize exposure of implementation details.

Refs: #473
  • Loading branch information
yesh0 committed Jan 2, 2023
1 parent 81bc307 commit ac49827
Show file tree
Hide file tree
Showing 21 changed files with 621 additions and 589 deletions.
7 changes: 7 additions & 0 deletions aya-obj/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,10 @@ documentation = "https://docs.rs/aya-obj"
edition = "2021"

[dependencies]
bytes = "1"
log = "0.4"
object = { version = "0.30", default-features = false, features = ["std", "read_core", "elf"] }
thiserror = "1"

[dev-dependencies]
matches = "0.1.8"
51 changes: 30 additions & 21 deletions aya/src/obj/btf/btf.rs → aya-obj/src/btf/btf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,13 @@ use thiserror::Error;

use crate::{
generated::{btf_ext_header, btf_header},
obj::btf::{
btf::{
info::{FuncSecInfo, LineSecInfo},
relocation::Relocation,
Array, BtfEnum, BtfKind, BtfMember, BtfType, Const, Enum, FuncInfo, FuncLinkage, Int,
IntEncoding, LineInfo, Struct, Typedef, VarLinkage,
},
util::bytes_of,
Features,
};

pub(crate) const MAX_RESOLVE_DEPTH: u8 = 32;
Expand Down Expand Up @@ -158,6 +157,16 @@ pub enum BtfError {
InvalidSymbolName,
}

#[derive(Default, Debug)]
pub struct BtfFeatures {
pub btf_func: bool,
pub btf_func_global: bool,
pub btf_datasec: bool,
pub btf_float: bool,
pub btf_decl_tag: bool,
pub btf_type_tag: bool,
}

/// Bpf Type Format metadata.
///
/// BTF is a kind of debug metadata that allows eBPF programs compiled against one kernel version
Expand All @@ -175,7 +184,7 @@ pub struct Btf {
}

impl Btf {
pub(crate) fn new() -> Btf {
pub fn new() -> Btf {
Btf {
header: btf_header {
magic: 0xeb9f,
Expand All @@ -197,15 +206,15 @@ impl Btf {
self.types.types.iter()
}

pub(crate) fn add_string(&mut self, name: String) -> u32 {
pub fn add_string(&mut self, name: String) -> u32 {
let str = CString::new(name).unwrap();
let name_offset = self.strings.len();
self.strings.extend(str.as_c_str().to_bytes_with_nul());
self.header.str_len = self.strings.len() as u32;
name_offset as u32
}

pub(crate) fn add_type(&mut self, btf_type: BtfType) -> u32 {
pub fn add_type(&mut self, btf_type: BtfType) -> u32 {
let size = btf_type.type_info_size() as u32;
let type_id = self.types.len();
self.types.push(btf_type);
Expand All @@ -231,7 +240,7 @@ impl Btf {
)
}

pub(crate) fn parse(data: &[u8], endianness: Endianness) -> Result<Btf, BtfError> {
pub fn parse(data: &[u8], endianness: Endianness) -> Result<Btf, BtfError> {
if data.len() < mem::size_of::<btf_header>() {
return Err(BtfError::InvalidHeader);
}
Expand Down Expand Up @@ -324,7 +333,7 @@ impl Btf {
self.string_at(ty.name_offset()).ok().map(String::from)
}

pub(crate) fn id_by_type_name_kind(&self, name: &str, kind: BtfKind) -> Result<u32, BtfError> {
pub fn id_by_type_name_kind(&self, name: &str, kind: BtfKind) -> Result<u32, BtfError> {
for (type_id, ty) in self.types().enumerate() {
if ty.kind() != kind {
continue;
Expand Down Expand Up @@ -370,7 +379,7 @@ impl Btf {
})
}

pub(crate) fn to_bytes(&self) -> Vec<u8> {
pub fn to_bytes(&self) -> Vec<u8> {
// Safety: btf_header is POD
let mut buf = unsafe { bytes_of::<btf_header>(&self.header).to_vec() };
// Skip the first type since it's always BtfType::Unknown for type_by_id to work
Expand All @@ -379,11 +388,11 @@ impl Btf {
buf
}

pub(crate) fn fixup_and_sanitize(
pub fn fixup_and_sanitize(
&mut self,
section_sizes: &HashMap<String, u64>,
symbol_offsets: &HashMap<String, u64>,
features: &Features,
features: &BtfFeatures,
) -> Result<(), BtfError> {
let mut types = mem::take(&mut self.types);
for i in 0..types.types.len() {
Expand Down Expand Up @@ -863,7 +872,7 @@ pub(crate) struct SecInfo<'a> {

#[cfg(test)]
mod tests {
use crate::obj::btf::{
use crate::btf::{
BtfParam, DataSec, DataSecEntry, DeclTag, Float, Func, FuncProto, Ptr, TypeTag, Var,
};

Expand Down Expand Up @@ -996,7 +1005,7 @@ mod tests {
let name_offset = btf.add_string("&mut int".to_string());
let ptr_type_id = btf.add_type(BtfType::Ptr(Ptr::new(name_offset, int_type_id)));

let features = Features {
let features = BtfFeatures {
..Default::default()
};

Expand Down Expand Up @@ -1034,7 +1043,7 @@ mod tests {
VarLinkage::Static,
)));

let features = Features {
let features = BtfFeatures {
btf_datasec: false,
..Default::default()
};
Expand Down Expand Up @@ -1078,7 +1087,7 @@ mod tests {
let datasec_type_id =
btf.add_type(BtfType::DataSec(DataSec::new(name_offset, variables, 0)));

let features = Features {
let features = BtfFeatures {
btf_datasec: false,
..Default::default()
};
Expand Down Expand Up @@ -1125,7 +1134,7 @@ mod tests {
let datasec_type_id =
btf.add_type(BtfType::DataSec(DataSec::new(name_offset, variables, 0)));

let features = Features {
let features = BtfFeatures {
btf_datasec: true,
..Default::default()
};
Expand Down Expand Up @@ -1186,7 +1195,7 @@ mod tests {
FuncLinkage::Static,
)));

let features = Features {
let features = BtfFeatures {
btf_func: false,
..Default::default()
};
Expand Down Expand Up @@ -1235,7 +1244,7 @@ mod tests {
let func_proto = BtfType::FuncProto(FuncProto::new(params, int_type_id));
let func_proto_type_id = btf.add_type(func_proto);

let features = Features {
let features = BtfFeatures {
btf_func: true,
..Default::default()
};
Expand Down Expand Up @@ -1284,7 +1293,7 @@ mod tests {
FuncLinkage::Global,
)));

let features = Features {
let features = BtfFeatures {
btf_func: true,
btf_func_global: false,
..Default::default()
Expand All @@ -1309,7 +1318,7 @@ mod tests {
let name_offset = btf.add_string("float".to_string());
let float_type_id = btf.add_type(BtfType::Float(Float::new(name_offset, 16)));

let features = Features {
let features = BtfFeatures {
btf_float: false,
..Default::default()
};
Expand Down Expand Up @@ -1349,7 +1358,7 @@ mod tests {
let decl_tag_type_id =
btf.add_type(BtfType::DeclTag(DeclTag::new(name_offset, var_type_id, -1)));

let features = Features {
let features = BtfFeatures {
btf_decl_tag: false,
..Default::default()
};
Expand Down Expand Up @@ -1377,7 +1386,7 @@ mod tests {
let type_tag_type = btf.add_type(BtfType::TypeTag(TypeTag::new(name_offset, int_type_id)));
btf.add_type(BtfType::Ptr(Ptr::new(0, type_tag_type)));

let features = Features {
let features = BtfFeatures {
btf_type_tag: false,
..Default::default()
};
Expand Down
16 changes: 8 additions & 8 deletions aya/src/obj/btf/info.rs → aya-obj/src/btf/info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use object::Endianness;

use crate::{
generated::{bpf_func_info, bpf_line_info},
obj::relocation::INS_SIZE,
relocation::INS_SIZE,
util::bytes_of,
};

Expand All @@ -20,7 +20,7 @@ use crate::{
* ......
*/
#[derive(Debug, Clone, Default)]
pub(crate) struct FuncSecInfo {
pub struct FuncSecInfo {
pub _sec_name_offset: u32,
pub num_info: u32,
pub func_info: Vec<bpf_func_info>,
Expand Down Expand Up @@ -64,7 +64,7 @@ impl FuncSecInfo {
}
}

pub(crate) fn func_info_bytes(&self) -> Vec<u8> {
pub fn func_info_bytes(&self) -> Vec<u8> {
let mut buf = vec![];
for l in &self.func_info {
// Safety: bpf_func_info is POD
Expand All @@ -73,13 +73,13 @@ impl FuncSecInfo {
buf
}

pub(crate) fn len(&self) -> usize {
pub fn len(&self) -> usize {
self.func_info.len()
}
}

#[derive(Debug, Clone)]
pub(crate) struct FuncInfo {
pub struct FuncInfo {
pub data: HashMap<String, FuncSecInfo>,
}

Expand All @@ -99,7 +99,7 @@ impl FuncInfo {
}

#[derive(Debug, Clone, Default)]
pub(crate) struct LineSecInfo {
pub struct LineSecInfo {
// each line info section has a header
pub _sec_name_offset: u32,
pub num_info: u32,
Expand Down Expand Up @@ -154,7 +154,7 @@ impl LineSecInfo {
}
}

pub(crate) fn line_info_bytes(&self) -> Vec<u8> {
pub fn line_info_bytes(&self) -> Vec<u8> {
let mut buf = vec![];
for l in &self.line_info {
// Safety: bpf_func_info is POD
Expand All @@ -163,7 +163,7 @@ impl LineSecInfo {
buf
}

pub(crate) fn len(&self) -> usize {
pub fn len(&self) -> usize {
self.line_info.len()
}
}
Expand Down
2 changes: 2 additions & 0 deletions aya/src/obj/btf/mod.rs → aya-obj/src/btf/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//! BTF loading, parsing and relocation.

#[allow(clippy::module_inception)]
mod btf;
mod info;
Expand Down
Loading

0 comments on commit ac49827

Please sign in to comment.