Skip to content

Commit fb567a3

Browse files
committed
Introduce ElfN_Nhdr type
The Elf32_Nhdr and Elf64_Nhdr types have the same size and layout. So far we have not exploited this fact and just stuck to the existing logic of differentiating between 32 and 64 bit types depending on the ELF bitness, as we do elsewhere. However, there is a certain amount of complexity associated with supporting the two bitnesses. With this change we introduce the ElfN_Nhdr type as an alias to Elf64_Nhdr and rely solely on it. Signed-off-by: Daniel Müller <deso@posteo.net>
1 parent c4e3f41 commit fb567a3

File tree

2 files changed

+10
-16
lines changed

2 files changed

+10
-16
lines changed

src/elf/types.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use std::borrow::Cow;
2+
use std::mem::size_of;
23

34
use crate::util::Either;
45
use crate::util::Pod;
@@ -613,6 +614,10 @@ impl Has32BitTy for Elf64_Nhdr {
613614
type Ty32Bit = Elf32_Nhdr;
614615
}
615616

617+
const _: () = assert!(size_of::<Elf32_Nhdr>() == size_of::<Elf64_Nhdr>());
618+
619+
pub(crate) type ElfN_Nhdr = Elf64_Nhdr;
620+
616621

617622
#[derive(Clone, Debug)]
618623
#[repr(C)]

src/normalize/buildid.rs

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@ use std::borrow::Cow;
22
use std::path::Path;
33

44
use crate::elf;
5-
use crate::elf::types::Elf32_Nhdr;
6-
use crate::elf::types::Elf64_Nhdr;
5+
use crate::elf::types::ElfN_Nhdr;
76
use crate::elf::ElfParser;
87
use crate::file_cache::FileCache;
98
use crate::log::warn;
@@ -27,14 +26,9 @@ fn read_build_id_from_notes(parser: &ElfParser) -> Result<Option<BuildId<'_>>> {
2726
// SANITY: We just found the index so the section data should always
2827
// be found.
2928
let mut bytes = parser.section_data(idx).unwrap();
30-
let (n_type, n_namesz, n_descsz) = if shdr.is_32bit() {
29+
let (n_type, n_namesz, n_descsz) = {
3130
let nhdr = bytes
32-
.read_pod_ref::<Elf32_Nhdr>()
33-
.ok_or_invalid_data(|| "failed to read build ID section header")?;
34-
(nhdr.n_type, nhdr.n_namesz, nhdr.n_descsz)
35-
} else {
36-
let nhdr = bytes
37-
.read_pod_ref::<Elf64_Nhdr>()
31+
.read_pod_ref::<ElfN_Nhdr>()
3832
.ok_or_invalid_data(|| "failed to read build ID section header")?;
3933
(nhdr.n_type, nhdr.n_namesz, nhdr.n_descsz)
4034
};
@@ -75,14 +69,9 @@ fn read_build_id_from_section_name(parser: &ElfParser) -> Result<Option<BuildId<
7569
// SANITY: We just found the index so the section should always be
7670
// found.
7771
let mut bytes = parser.section_data(idx).unwrap();
78-
let (n_namesz, n_descsz) = if shdr.is_32bit() {
79-
let nhdr = bytes
80-
.read_pod_ref::<Elf32_Nhdr>()
81-
.ok_or_invalid_data(|| "failed to read build ID section header")?;
82-
(nhdr.n_namesz, nhdr.n_descsz)
83-
} else {
72+
let (n_namesz, n_descsz) = {
8473
let nhdr = bytes
85-
.read_pod_ref::<Elf64_Nhdr>()
74+
.read_pod_ref::<ElfN_Nhdr>()
8675
.ok_or_invalid_data(|| "failed to read build ID section header")?;
8776
(nhdr.n_namesz, nhdr.n_descsz)
8877
};

0 commit comments

Comments
 (0)