Skip to content

Commit 91cb766

Browse files
committed
fixup! ELF relocations
1 parent 31624d0 commit 91cb766

File tree

2 files changed

+36
-8
lines changed

2 files changed

+36
-8
lines changed

src/elf/parser.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1066,7 +1066,7 @@ where
10661066
.context("failed to read ELF RELA entries")?;
10671067

10681068
for rela in entries.iter() {
1069-
let r_type = rela.r_info & 0xff;
1069+
let (r_type, sym_idx) = rela.dissect_info();
10701070
if r_type == 0 {
10711071
continue;
10721072
}
@@ -1077,7 +1077,6 @@ where
10771077
ehdr.ehdr.machine()
10781078
);
10791079
}
1080-
let sym_idx = (rela.r_info >> 8) as usize;
10811080
let sym_value = if sym_idx > 0 {
10821081
let sym = syms
10831082
.get(sym_idx)
@@ -1106,7 +1105,7 @@ where
11061105
.context("failed to read ELF REL entries")?;
11071106

11081107
for rel in entries.iter() {
1109-
let r_type = rel.r_info & 0xff;
1108+
let (r_type, sym_idx) = rel.dissect_info();
11101109
if r_type == 0 {
11111110
continue;
11121111
}
@@ -1117,7 +1116,6 @@ where
11171116
ehdr.ehdr.machine()
11181117
);
11191118
}
1120-
let sym_idx = (rel.r_info >> 8) as usize;
11211119
let sym_value = if sym_idx > 0 {
11221120
let sym = syms
11231121
.get(sym_idx)
@@ -1154,7 +1152,7 @@ where
11541152
.context("failed to read ELF RELA entries")?;
11551153

11561154
for rela in entries.iter() {
1157-
let r_type = (rela.r_info & 0xffffffff) as u32;
1155+
let (r_type, sym_idx) = rela.dissect_info();
11581156
if r_type == 0 {
11591157
continue;
11601158
}
@@ -1165,7 +1163,6 @@ where
11651163
ehdr.ehdr.machine()
11661164
);
11671165
}
1168-
let sym_idx = (rela.r_info >> 32) as usize;
11691166
let sym_value = if sym_idx > 0 {
11701167
let sym = syms
11711168
.get(sym_idx)
@@ -1194,7 +1191,7 @@ where
11941191
.context("failed to read ELF REL entries")?;
11951192

11961193
for rel in entries.iter() {
1197-
let r_type = (rel.r_info & 0xffffffff) as u32;
1194+
let (r_type, sym_idx) = rel.dissect_info();
11981195
if r_type == 0 {
11991196
continue;
12001197
}
@@ -1205,7 +1202,6 @@ where
12051202
ehdr.ehdr.machine()
12061203
);
12071204
}
1208-
let sym_idx = (rel.r_info >> 32) as usize;
12091205
let sym_value = if sym_idx > 0 {
12101206
let sym = syms
12111207
.get(sym_idx)

src/elf/types.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -739,6 +739,14 @@ pub(crate) struct Elf32_Rel {
739739
pub r_info: Elf32_Word,
740740
}
741741

742+
impl Elf32_Rel {
743+
pub fn dissect_info(&self) -> (u32, usize) {
744+
let r_type = self.r_info & 0xff;
745+
let sym_idx = (self.r_info >> 8) as usize;
746+
(r_type, sym_idx)
747+
}
748+
}
749+
742750
// SAFETY: `Elf32_Rel` is valid for any bit pattern.
743751
unsafe impl Pod for Elf32_Rel {}
744752

@@ -750,6 +758,14 @@ pub(crate) struct Elf64_Rel {
750758
pub r_info: Elf64_Xword,
751759
}
752760

761+
impl Elf64_Rel {
762+
pub fn dissect_info(&self) -> (u32, usize) {
763+
let r_type = (self.r_info & 0xffffffff) as u32;
764+
let sym_idx = (self.r_info >> 32) as usize;
765+
(r_type, sym_idx)
766+
}
767+
}
768+
753769
// SAFETY: `Elf64_Rel` is valid for any bit pattern.
754770
unsafe impl Pod for Elf64_Rel {}
755771

@@ -766,6 +782,14 @@ pub(crate) struct Elf32_Rela {
766782
pub r_addend: i32,
767783
}
768784

785+
impl Elf32_Rela {
786+
pub fn dissect_info(&self) -> (u32, usize) {
787+
let r_type = self.r_info & 0xff;
788+
let sym_idx = (self.r_info >> 8) as usize;
789+
(r_type, sym_idx)
790+
}
791+
}
792+
769793
// SAFETY: `Elf32_Rela` is valid for any bit pattern.
770794
unsafe impl Pod for Elf32_Rela {}
771795

@@ -778,6 +802,14 @@ pub(crate) struct Elf64_Rela {
778802
pub r_addend: i64,
779803
}
780804

805+
impl Elf64_Rela {
806+
pub fn dissect_info(&self) -> (u32, usize) {
807+
let r_type = (self.r_info & 0xffffffff) as u32;
808+
let sym_idx = (self.r_info >> 32) as usize;
809+
(r_type, sym_idx)
810+
}
811+
}
812+
781813
// SAFETY: `Elf64_Rela` is valid for any bit pattern.
782814
unsafe impl Pod for Elf64_Rela {}
783815

0 commit comments

Comments
 (0)