Skip to content

Release 0.18.0 #207

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

Merged
merged 3 commits into from
Mar 22, 2020
Merged
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
20 changes: 10 additions & 10 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
[package]
authors = ["Nick Fitzgerald <[email protected]>", "Philip Craig <[email protected]>"]
name = "object"
version = "0.17.0"
version = "0.18.0"
authors = ["Nick Fitzgerald <[email protected]>", "Philip Craig <[email protected]>"]
edition = "2018"
description = "A unified interface for reading and writing object file formats."
exclude = ["/.coveralls.yml", "/.travis.yml"]
keywords = ["object", "elf", "mach-o", "pe", "coff"]
license = "Apache-2.0/MIT"
repository = "https://github.com/gimli-rs/object"
exclude = ["/.coveralls.yml", "/.travis.yml"]
description = "A unified interface for reading and writing object file formats."

[package.metadata.docs.rs]
all-features = true

[dependencies]
target-lexicon = { version = "0.10" }
flate2 = { version = "1", optional = true }
crc32fast = { version = "1.2", optional = true }
flate2 = { version = "1", optional = true }
indexmap = { version = "1.1", optional = true }
target-lexicon = { version = "0.10" }
wasmparser = { version = "0.51.0", optional = true }

[dev-dependencies]
Expand All @@ -39,14 +39,14 @@ wasm = ["wasmparser"]

default = ["read", "compression"]

[[example]]
name = "objdump"
required-features = ["read"]

[[example]]
name = "nm"
required-features = ["read"]

[[example]]
name = "objcopy"
required-features = ["read", "write"]

[[example]]
name = "objdump"
required-features = ["read"]
157 changes: 83 additions & 74 deletions src/read/elf/section.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
#[cfg(feature = "compression")]
use alloc::borrow::Cow;
use alloc::vec::Vec;
use core::fmt::Debug;
use core::{iter, mem, slice, str};
#[cfg(feature = "compression")]
use flate2::{Decompress, FlushDecompress};

use crate::elf;
use crate::endian::{self, RunTimeEndian, U32};
use crate::endian::{self, RunTimeEndian};
use crate::pod::{Bytes, Pod};
use crate::read::{self, Error, ObjectSection, ReadError, SectionFlags, SectionIndex, SectionKind};
use crate::read::{self, ObjectSection, ReadError, SectionFlags, SectionIndex, SectionKind};

use super::{CompressionHeader, ElfFile, ElfNoteIterator, ElfRelocationIterator, FileHeader};
use super::{ElfFile, ElfNoteIterator, ElfRelocationIterator, FileHeader};

/// An iterator over the sections of an `ElfFile32`.
pub type ElfSectionIterator32<'data, 'file, Endian = RunTimeEndian> =
Expand Down Expand Up @@ -68,74 +65,6 @@ impl<'data, 'file, Elf: FileHeader> ElfSection<'data, 'file, Elf> {
.data(self.file.endian, self.file.data)
.read_error("Invalid ELF section size or offset")
}

#[cfg(feature = "compression")]
fn maybe_decompress_data(&self) -> read::Result<Option<Cow<'data, [u8]>>> {
let endian = self.file.endian;
if (self.section.sh_flags(endian).into() & u64::from(elf::SHF_COMPRESSED)) == 0 {
return Ok(None);
}

let mut data = self
.section
.data(endian, self.file.data)
.read_error("Invalid ELF compressed section offset or size")?;
let header = data
.read::<Elf::CompressionHeader>()
.read_error("Invalid ELF compression header size or alignment")?;
if header.ch_type(endian) != elf::ELFCOMPRESS_ZLIB {
return Err(Error("Unsupported ELF compression type"));
}

let uncompressed_size: u64 = header.ch_size(endian).into();
let mut decompressed = Vec::with_capacity(uncompressed_size as usize);
let mut decompress = Decompress::new(true);
if decompress
.decompress_vec(data.0, &mut decompressed, FlushDecompress::Finish)
.is_err()
{
return Err(Error("Invalid ELF compressed data"));
}
Ok(Some(Cow::Owned(decompressed)))
}

/// Try GNU-style "ZLIB" header decompression.
#[cfg(feature = "compression")]
fn maybe_decompress_data_gnu(&self) -> read::Result<Option<Cow<'data, [u8]>>> {
let name = match self.name() {
Ok(name) => name,
// I think it's ok to ignore this error?
Err(_) => return Ok(None),
};
if !name.starts_with(".zdebug_") {
return Ok(None);
}
let mut data = self.bytes()?;
// Assume ZLIB-style uncompressed data is no more than 4GB to avoid accidentally
// huge allocations. This also reduces the chance of accidentally matching on a
// .debug_str that happens to start with "ZLIB".
if data
.read_bytes(8)
.read_error("ELF GNU compressed section is too short")?
.0
!= b"ZLIB\0\0\0\0"
{
return Err(Error("Invalid ELF GNU compressed section header"));
}
let uncompressed_size = data
.read::<U32<_>>()
.read_error("ELF GNU compressed section is too short")?
.get(endian::BigEndian);
let mut decompressed = Vec::with_capacity(uncompressed_size as usize);
let mut decompress = Decompress::new(true);
if decompress
.decompress_vec(data.0, &mut decompressed, FlushDecompress::Finish)
.is_err()
{
return Err(Error("Invalid ELF GNU compressed data"));
}
Ok(Some(Cow::Owned(decompressed)))
}
}

impl<'data, 'file, Elf: FileHeader> read::private::Sealed for ElfSection<'data, 'file, Elf> {}
Expand Down Expand Up @@ -454,3 +383,83 @@ impl<Endian: endian::Endian> SectionHeader for elf::SectionHeader64<Endian> {
self.sh_entsize.get(endian)
}
}

#[cfg(feature = "compression")]
mod compression {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice.

use alloc::vec::Vec;
use flate2::{Decompress, FlushDecompress};

use crate::endian::U32;
use crate::read::elf::CompressionHeader;
use crate::read::Error;

use super::*;

impl<'data, 'file, Elf: FileHeader> ElfSection<'data, 'file, Elf> {
pub(super) fn maybe_decompress_data(&self) -> read::Result<Option<Cow<'data, [u8]>>> {
let endian = self.file.endian;
if (self.section.sh_flags(endian).into() & u64::from(elf::SHF_COMPRESSED)) == 0 {
return Ok(None);
}

let mut data = self
.section
.data(endian, self.file.data)
.read_error("Invalid ELF compressed section offset or size")?;
let header = data
.read::<Elf::CompressionHeader>()
.read_error("Invalid ELF compression header size or alignment")?;
if header.ch_type(endian) != elf::ELFCOMPRESS_ZLIB {
return Err(Error("Unsupported ELF compression type"));
}

let uncompressed_size: u64 = header.ch_size(endian).into();
let mut decompressed = Vec::with_capacity(uncompressed_size as usize);
let mut decompress = Decompress::new(true);
if decompress
.decompress_vec(data.0, &mut decompressed, FlushDecompress::Finish)
.is_err()
{
return Err(Error("Invalid ELF compressed data"));
}
Ok(Some(Cow::Owned(decompressed)))
}

/// Try GNU-style "ZLIB" header decompression.
pub(super) fn maybe_decompress_data_gnu(&self) -> read::Result<Option<Cow<'data, [u8]>>> {
let name = match self.name() {
Ok(name) => name,
// I think it's ok to ignore this error?
Err(_) => return Ok(None),
};
if !name.starts_with(".zdebug_") {
return Ok(None);
}
let mut data = self.bytes()?;
// Assume ZLIB-style uncompressed data is no more than 4GB to avoid accidentally
// huge allocations. This also reduces the chance of accidentally matching on a
// .debug_str that happens to start with "ZLIB".
if data
.read_bytes(8)
.read_error("ELF GNU compressed section is too short")?
.0
!= b"ZLIB\0\0\0\0"
{
return Err(Error("Invalid ELF GNU compressed section header"));
}
let uncompressed_size = data
.read::<U32<_>>()
.read_error("ELF GNU compressed section is too short")?
.get(endian::BigEndian);
let mut decompressed = Vec::with_capacity(uncompressed_size as usize);
let mut decompress = Decompress::new(true);
if decompress
.decompress_vec(data.0, &mut decompressed, FlushDecompress::Finish)
.is_err()
{
return Err(Error("Invalid ELF GNU compressed data"));
}
Ok(Some(Cow::Owned(decompressed)))
}
}
}