Skip to content

Commit 5541355

Browse files
authored
Remove the dependency on xmas_elf (#821)
1 parent c6c0e9b commit 5541355

File tree

16 files changed

+38
-66
lines changed

16 files changed

+38
-66
lines changed

Cargo.lock

Lines changed: 0 additions & 16 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

espflash/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ strum = { version = "0.26.3", features = ["derive"] }
5656
thiserror = "2.0.10"
5757
toml = { version = "0.8.19", optional = true }
5858
update-informer = { version = "1.2.0", optional = true }
59-
xmas-elf = "0.9.1"
6059

6160
[target.'cfg(unix)'.dependencies]
6261
libc = "0.2.169"

espflash/src/cli/mod.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ use esp_idf_part::{DataType, Partition, PartitionTable};
2525
use indicatif::{style::ProgressStyle, HumanCount, ProgressBar};
2626
use log::{debug, info, warn};
2727
use miette::{IntoDiagnostic, Result, WrapErr};
28+
use object::read::elf::ElfFile32 as ElfFile;
2829
use serialport::{FlowControl, SerialPortInfo, SerialPortType, UsbPortInfo};
29-
use xmas_elf::ElfFile;
3030

3131
use self::{
3232
config::Config,
@@ -35,7 +35,7 @@ use self::{
3535
};
3636
use crate::{
3737
connection::reset::{ResetAfterOperation, ResetBeforeOperation},
38-
error::{ElfError, Error, MissingPartition, MissingPartitionTable},
38+
error::{Error, MissingPartition, MissingPartitionTable},
3939
flasher::{
4040
FlashData,
4141
FlashFrequency,
@@ -607,9 +607,7 @@ pub fn save_elf_as_image(
607607
skip_padding: bool,
608608
xtal_freq: XtalFrequency,
609609
) -> Result<()> {
610-
let elf = ElfFile::new(elf_data)
611-
.map_err(ElfError::from)
612-
.into_diagnostic()?;
610+
let elf = ElfFile::parse(elf_data).into_diagnostic()?;
613611

614612
if merge {
615613
// To get a chip revision, the connection is needed

espflash/src/error.rs

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ pub enum Error {
181181
code(espflash::invalid_elf),
182182
help("Try running `cargo clean` and rebuilding the image")
183183
)]
184-
InvalidElf(#[from] ElfError),
184+
InvalidElf(#[from] object::Error),
185185

186186
#[error("The bootloader returned an error")]
187187
#[cfg(feature = "serialport")]
@@ -492,17 +492,6 @@ impl From<String> for MissingPartition {
492492
)]
493493
pub struct MissingPartitionTable;
494494

495-
/// Invalid ELF file error
496-
#[derive(Debug, Error)]
497-
#[error("{0}")]
498-
pub struct ElfError(&'static str);
499-
500-
impl From<&'static str> for ElfError {
501-
fn from(err: &'static str) -> Self {
502-
ElfError(err)
503-
}
504-
}
505-
506495
#[cfg(feature = "serialport")]
507496
pub(crate) trait ResultExt {
508497
/// Mark an error as having occurred during the flashing stage

espflash/src/flasher/mod.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@ use esp_idf_part::PartitionTable;
1313
use log::{debug, info, warn};
1414
#[cfg(feature = "serialport")]
1515
use md5::{Digest, Md5};
16+
#[cfg(feature = "serialport")]
17+
use object::{read::elf::ElfFile32 as ElfFile, Endianness};
1618
use serde::{Deserialize, Serialize};
1719
#[cfg(feature = "serialport")]
1820
use serialport::UsbPortInfo;
1921
use strum::{Display, EnumIter, IntoEnumIterator, VariantNames};
20-
#[cfg(feature = "serialport")]
21-
use xmas_elf::ElfFile;
2222

2323
#[cfg(feature = "serialport")]
2424
pub(crate) use self::stubs::{FLASH_SECTOR_SIZE, FLASH_WRITE_SIZE};
@@ -32,7 +32,7 @@ use crate::{
3232
Connection,
3333
Port,
3434
},
35-
error::{ConnectionError, ElfError, ResultExt as _},
35+
error::{ConnectionError, ResultExt as _},
3636
flasher::stubs::{
3737
FlashStub,
3838
CHIP_DETECT_MAGIC_REG_ADDR,
@@ -1011,13 +1011,13 @@ impl Flasher {
10111011
elf_data: &[u8],
10121012
mut progress: Option<&mut dyn ProgressCallbacks>,
10131013
) -> Result<(), Error> {
1014-
let elf = ElfFile::new(elf_data).map_err(ElfError::from)?;
1014+
let elf = ElfFile::parse(elf_data)?;
10151015
if rom_segments(self.chip, &elf).next().is_some() {
10161016
return Err(Error::ElfNotRamLoadable);
10171017
}
10181018

10191019
let mut target = self.chip.ram_target(
1020-
Some(elf.header.pt2.entry_point() as u32),
1020+
Some(elf.elf_header().e_entry.get(Endianness::Little)),
10211021
self.chip
10221022
.into_target()
10231023
.max_ram_block_size(&mut self.connection)?,
@@ -1041,7 +1041,7 @@ impl Flasher {
10411041
mut progress: Option<&mut dyn ProgressCallbacks>,
10421042
xtal_freq: XtalFrequency,
10431043
) -> Result<(), Error> {
1044-
let elf = ElfFile::new(elf_data).map_err(ElfError::from)?;
1044+
let elf = ElfFile::parse(elf_data)?;
10451045

10461046
let mut target =
10471047
self.chip

espflash/src/image_format/esp_idf.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ use std::{borrow::Cow, io::Write, iter::once, mem::size_of};
44

55
use bytemuck::{bytes_of, from_bytes, Pod, Zeroable};
66
use esp_idf_part::{AppType, DataType, Partition, PartitionTable, SubType, Type};
7+
use object::{read::elf::ElfFile32 as ElfFile, Endianness};
78
use sha2::{Digest, Sha256};
8-
use xmas_elf::ElfFile;
99

1010
use super::{ram_segments, rom_segments, Segment};
1111
use crate::{
@@ -190,7 +190,7 @@ impl<'a> IdfBootloaderFormat<'a> {
190190
// write the header of the app
191191
// use the same settings as the bootloader
192192
// just update the entry point
193-
header.entry = elf.header.pt2.entry_point() as u32;
193+
header.entry = elf.elf_header().e_entry.get(Endianness::Little);
194194
header.wp_pin = WP_PIN_DISABLED;
195195
header.chip_id = params.chip_id;
196196
header.min_chip_rev_full = flash_data.min_chip_rev;

espflash/src/image_format/mod.rs

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,12 @@ use std::{
88
ops::AddAssign,
99
};
1010

11-
use xmas_elf::{
12-
sections::{SectionData, ShType},
13-
ElfFile,
11+
use object::{
12+
elf::SHT_PROGBITS,
13+
read::elf::{ElfFile32 as ElfFile, SectionHeader},
14+
Endianness,
15+
Object as _,
16+
ObjectSection as _,
1417
};
1518

1619
pub use self::esp_idf::IdfBootloaderFormat;
@@ -163,19 +166,18 @@ pub(crate) fn rom_segments<'a>(
163166

164167
fn segments<'a>(elf: &'a ElfFile<'a>) -> Box<dyn Iterator<Item = Segment<'a>> + 'a> {
165168
Box::new(
166-
elf.section_iter()
167-
.filter(|header| {
168-
header.size() > 0
169-
&& header.get_type() == Ok(ShType::ProgBits)
170-
&& header.offset() > 0
171-
&& header.address() > 0
169+
elf.sections()
170+
.filter(|section| {
171+
let header = section.elf_section_header();
172+
173+
section.size() > 0
174+
&& header.sh_type(Endianness::Little) == SHT_PROGBITS
175+
&& header.sh_offset.get(Endianness::Little) > 0
176+
&& section.address() > 0
172177
})
173-
.flat_map(move |header| {
174-
let addr = header.address() as u32;
175-
match header.get_data(elf) {
176-
Ok(SectionData::Undefined(data)) => Some(Segment::new(addr, data)),
177-
_ => None,
178-
}
178+
.flat_map(move |section| match section.data() {
179+
Ok(data) => Some(Segment::new(section.address() as u32, data)),
180+
_ => None,
179181
}),
180182
)
181183
}

espflash/src/targets/esp32.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::ops::Range;
22

3-
use xmas_elf::ElfFile;
3+
use object::read::elf::ElfFile32 as ElfFile;
44

55
#[cfg(feature = "serialport")]
66
use crate::{connection::Connection, targets::bytes_to_mac_addr};

espflash/src/targets/esp32c2.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::{collections::HashMap, ops::Range};
22

33
use log::debug;
4-
use xmas_elf::ElfFile;
4+
use object::read::elf::ElfFile32 as ElfFile;
55

66
#[cfg(feature = "serialport")]
77
use crate::{connection::Connection, targets::bytes_to_mac_addr};

espflash/src/targets/esp32c3.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::ops::Range;
22

3-
use xmas_elf::ElfFile;
3+
use object::read::elf::ElfFile32 as ElfFile;
44

55
#[cfg(feature = "serialport")]
66
use crate::connection::Connection;

0 commit comments

Comments
 (0)