Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for custom permissions for custom sections. #105

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
146 changes: 144 additions & 2 deletions src/artifact/decl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,11 +214,29 @@ impl DefinedDecl {
pub fn is_writable(&self) -> bool {
match self {
DefinedDecl::Data(a) => a.is_writable(),
DefinedDecl::Function(_) => false,
DefinedDecl::Function(a) => a.is_writable(),
DefinedDecl::Section(a) => a.is_writable(),
}
}

/// Accessor to determine whether contents are executable
pub fn is_executable(&self) -> bool {
match self {
DefinedDecl::Section(a) => a.is_executable(),
DefinedDecl::Function(_) => true,
DefinedDecl::Data(a) => a.is_executable(),
}
}

/// Accessor to determine whether contents will be loaded at runtime
pub fn is_loaded(&self) -> bool {
match self {
DefinedDecl::Section(a) => a.is_loaded(),
DefinedDecl::Function(_) => true,
DefinedDecl::Data(_) => true,
}
}

/// Accessor to determine the minimal alignment
pub fn get_align(&self) -> Option<u64> {
match self {
Expand Down Expand Up @@ -400,6 +418,7 @@ pub struct FunctionDecl {
scope: Scope,
visibility: Visibility,
align: Option<u64>,
writable: Option<bool>,
}

impl Default for FunctionDecl {
Expand All @@ -408,6 +427,7 @@ impl Default for FunctionDecl {
scope: Scope::Local,
visibility: Visibility::Default,
align: None,
writable: None,
}
}
}
Expand All @@ -416,6 +436,36 @@ impl FunctionDecl {
scope_methods!();
visibility_methods!();
align_methods!();

/// Setter for mutability
pub fn set_writable(&mut self, writable: bool) {
self.writable = Some(writable);
}

/// Builder for mutability
pub fn with_writable(mut self, writable: bool) -> Self {
self.writable = Some(writable);
self
}

/// Set mutability to writable
pub fn writable(self) -> Self {
self.with_writable(true)
}

/// Set mutability to read-only
pub fn read_only(self) -> Self {
self.with_writable(false)
}

/// Accessor to determine whether contents are writable
pub fn is_writable(&self) -> bool {
if let Some(writable) = self.writable {
return writable;
}

false
}
}

impl Into<Decl> for FunctionDecl {
Expand All @@ -430,6 +480,7 @@ pub struct DataDecl {
scope: Scope,
visibility: Visibility,
writable: bool,
executable: Option<bool>,
datatype: DataType,
align: Option<u64>,
}
Expand All @@ -440,6 +491,7 @@ impl Default for DataDecl {
scope: Scope::Local,
visibility: Visibility::Default,
writable: false,
executable: None,
datatype: DataType::Bytes,
align: None,
}
Expand All @@ -452,7 +504,7 @@ impl DataDecl {
datatype_methods!();
align_methods!();

/// Builder for writability
/// Builder for mutability
pub fn with_writable(mut self, writable: bool) -> Self {
self.writable = writable;
self
Expand All @@ -473,6 +525,26 @@ impl DataDecl {
pub fn is_writable(&self) -> bool {
self.writable
}

/// Setter for executability
pub fn set_executable(&mut self, executable: bool) {
self.executable = Some(executable);
}

/// Builder for executability
pub fn with_executable(mut self, executable: bool) -> Self {
self.executable = Some(executable);
self
}

/// Accessor to determine whether contents are executable
pub fn is_executable(&self) -> bool {
if let Some(executable) = self.executable {
return executable;
}

false
}
}

impl Into<Decl> for DataDecl {
Expand Down Expand Up @@ -500,6 +572,9 @@ pub struct SectionDecl {
kind: SectionKind,
datatype: DataType,
align: Option<u64>,
writable: Option<bool>,
executable: Option<bool>,
loaded: bool,
}

impl SectionDecl {
Expand All @@ -512,6 +587,9 @@ impl SectionDecl {
kind,
datatype: DataType::Bytes,
align: None,
writable: None,
executable: None,
loaded: false,
}
}

Expand All @@ -521,14 +599,78 @@ impl SectionDecl {
false
}

/// Setter for mutability
pub fn set_writable(&mut self, writable: bool) {
self.writable = Some(writable);
}

/// Builder for mutability
pub fn with_writable(mut self, writable: bool) -> Self {
self.writable = Some(writable);
self
}

/// Set mutability to writable
pub fn writable(self) -> Self {
self.with_writable(true)
}

/// Set mutability to read-only
pub fn read_only(self) -> Self {
self.with_writable(false)
}

/// Accessor to determine whether contents are writable
pub fn is_writable(&self) -> bool {
if let Some(writable) = self.writable {
return writable;
}

match self.kind {
SectionKind::Data => true,
kitlith marked this conversation as resolved.
Show resolved Hide resolved
SectionKind::Debug | SectionKind::Text => false,
}
}

/// Setter for executability
pub fn set_executable(&mut self, executable: bool) {
self.executable = Some(executable);
}

/// Builder for executability
pub fn with_executable(mut self, executable: bool) -> Self {
self.executable = Some(executable);
self
}

/// Accessor to determine whether contents are executable
pub fn is_executable(&self) -> bool {
if let Some(executable) = self.executable {
return executable;
}

match self.kind {
SectionKind::Text => true,
SectionKind::Data | SectionKind::Debug => false,
}
}

/// Setter for loadability
pub fn set_loaded(&mut self, loaded: bool) {
self.loaded = loaded;
}

/// Builder for loadabliity
pub fn with_loaded(mut self, loaded: bool) -> Self {
self.loaded = loaded;
self
}

/// Accessor to determine whether contents are loaded at runtime
pub fn is_loaded(&self) -> bool {
self.loaded
}

/// Get the kind for this `SectionDecl`
pub fn kind(&self) -> SectionKind {
self.kind
Expand Down
43 changes: 20 additions & 23 deletions src/elf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use crate::{
artifact::{
self, Artifact, Data, DataType, Decl, DefinedDecl, ImportKind, LinkAndDecl, Reloc, Scope,
Visibility,
SectionKind, Visibility,
},
target::make_ctx,
Ctx,
Expand Down Expand Up @@ -514,33 +514,30 @@ impl<'a> Elf<'a> {
(_, DefinedDecl::Section(_)) => name.to_owned(),
};

let section = match decl {
DefinedDecl::Function(d) => SectionBuilder::new(def_size as u64)
.section_type(SectionType::Bits)
.alloc()
.writable(false)
.exec(true)
.align(d.get_align()),
DefinedDecl::Data(d) => SectionBuilder::new(def_size as u64)
.section_type(Self::section_type_for_data(
d.get_datatype(),
def.data.is_zero_init(),
))
.alloc()
.writable(d.is_writable())
.exec(false)
.align(d.get_align()),
DefinedDecl::Section(d) => SectionBuilder::new(def_size as u64)
.section_type(
let mut section = SectionBuilder::new(def_size as u64)
.writable(decl.is_writable())
.exec(decl.is_executable())
.align(decl.get_align())
.section_type(match decl {
DefinedDecl::Function(_) => SectionType::Bits,
DefinedDecl::Data(d) => {
Self::section_type_for_data(d.get_datatype(), def.data.is_zero_init())
}
DefinedDecl::Section(d) => {
// TODO: this behavior should be deprecated, but we need to warn users!
if name == ".debug_str" || name == ".debug_line_str" {
SectionType::String
} else if d.kind() == SectionKind::Text && d.get_datatype() == DataType::Bytes {
SectionType::Bits
} else {
Self::section_type_for_data(d.get_datatype(), def.data.is_zero_init())
},
)
.align(d.get_align()),
};
}
}
});

if decl.is_loaded() {
section = section.alloc();
}

let shndx = match def.data {
Data::Blob(bytes) => self.add_progbits(section_name, section, bytes),
Expand Down
50 changes: 50 additions & 0 deletions tests/elf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,9 +225,59 @@ fn decl_attributes() {
Ok(())
},
),
DeclTestCase::new(
"executable_data",
Decl::data().with_executable(true),
|_sym, sect| {
ensure!(sect.is_executable(), "executable");
Ok(())
},
),
DeclTestCase::new(
"mutable_function",
Decl::function().writable(),
|_sym, sect| {
ensure!(sect.is_writable(), "writable");
Ok(())
},
),
]);
}

#[test]
// Can't test with DeclTestCase, as section declarations don't generate symbols
fn section_permissions() {
let mut obj = Artifact::new(triple!("x86_64-unknown-unknown-unknown-elf"), "a".into());
obj.declare(
"test",
Decl::section(faerie::SectionKind::Text)
.with_loaded(true)
.with_writable(true)
.with_executable(true),
)
.expect("Can declare section with permissions");
obj.define("test", vec![1, 2, 3, 4])
.expect("Can define section");

let bytes = obj.emit().expect("can emit elf file");
if let goblin::Object::Elf(elf) = goblin::Object::parse(&bytes).expect("can parse elf file") {
let sect = elf
.section_headers
.iter()
.find(|section| &elf.shdr_strtab[section.sh_name] == "test");

if let Some(section) = sect {
assert!(section.is_alloc());
assert!(section.is_writable());
assert!(section.is_executable());
} else {
panic!("Could not find test section")
}
} else {
panic!("Elf file not parsed as elf file");
}
}

/* test scaffolding: */

fn decl_tests(tests: Vec<DeclTestCase>) {
Expand Down