From 8dca05e27c9cb1526afb324711bd577b553d6d36 Mon Sep 17 00:00:00 2001 From: Calvin Lee Date: Sat, 8 Oct 2016 17:45:50 -0600 Subject: [PATCH 1/3] Added name method to ElfSection --- src/elf_sections.rs | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/src/elf_sections.rs b/src/elf_sections.rs index 62e06995..b1067541 100644 --- a/src/elf_sections.rs +++ b/src/elf_sections.rs @@ -62,6 +62,29 @@ pub struct ElfSection { } impl ElfSection { + pub fn name(&self, tag: &ElfSectionsTag) -> &'static str { + use core::{str, slice}; + + let strtabs = unsafe { + &*(&tag.first_section as *const ElfSection).offset(tag.shndx as isize) + }; + + let name_byte = (strtabs.addr + self.name as u64) as *const _; + + unsafe { + let mut strlen = 0; + for i in 0.. { + if *name_byte.offset(i) == 0 { + strlen = i as usize; + break; + } + } + + str::from_utf8_unchecked( + slice::from_raw_parts(name_byte, strlen)) + } + } + pub fn start_address(&self) -> usize { self.addr as usize } From 3ff6c7eb478d6e790ad034cc7930096b9ed3bf3d Mon Sep 17 00:00:00 2001 From: Calvin Lee Date: Tue, 11 Oct 2016 10:31:40 -0600 Subject: [PATCH 2/3] Fix explicit dereference of string table --- src/elf_sections.rs | 59 +++++++++++++++++++++++++++------------------ 1 file changed, 35 insertions(+), 24 deletions(-) diff --git a/src/elf_sections.rs b/src/elf_sections.rs index b1067541..0bdf92a6 100644 --- a/src/elf_sections.rs +++ b/src/elf_sections.rs @@ -18,6 +18,40 @@ impl ElfSectionsTag { entry_size: self.entry_size, } } + + pub fn string_table(&self) -> &'static StringTable { + unsafe { + let string_table_ptr = + (&self.first_section as *const ElfSection).offset(self.shndx as isize); + &*((*string_table_ptr).addr as *const StringTable) + } + } +} + +pub struct StringTable(u8); + +impl StringTable { + pub fn section_name(&self, section: &ElfSection) -> &'static str { + unsafe fn strlen(start: *const u8) -> usize { + for i in 0.. { + if *start.offset(i) == 0 { + return i as usize; + } + } + unreachable!() + } + use core::{str, slice}; + + let name_ptr = unsafe { + (&self.0 as *const u8).offset(section.name_index as isize) + }; + let strlen = unsafe { strlen(name_ptr) }; + + unsafe { + str::from_utf8( + slice::from_raw_parts(name_ptr, strlen)).unwrap() + } + } } #[derive(Clone)] @@ -49,7 +83,7 @@ impl Iterator for ElfSectionIter { #[derive(Debug)] #[repr(C)] pub struct ElfSection { - name: u32, + name_index: u32, typ: u32, pub flags: u64, pub addr: u64, @@ -62,29 +96,6 @@ pub struct ElfSection { } impl ElfSection { - pub fn name(&self, tag: &ElfSectionsTag) -> &'static str { - use core::{str, slice}; - - let strtabs = unsafe { - &*(&tag.first_section as *const ElfSection).offset(tag.shndx as isize) - }; - - let name_byte = (strtabs.addr + self.name as u64) as *const _; - - unsafe { - let mut strlen = 0; - for i in 0.. { - if *name_byte.offset(i) == 0 { - strlen = i as usize; - break; - } - } - - str::from_utf8_unchecked( - slice::from_raw_parts(name_byte, strlen)) - } - } - pub fn start_address(&self) -> usize { self.addr as usize } From 0a73d1b05956ca324d15f85ee2d3c09f28801c77 Mon Sep 17 00:00:00 2001 From: Calvin Lee Date: Tue, 11 Oct 2016 11:38:31 -0600 Subject: [PATCH 3/3] Reduce size of unsafe blocks --- src/elf_sections.rs | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/src/elf_sections.rs b/src/elf_sections.rs index 0bdf92a6..af1aa782 100644 --- a/src/elf_sections.rs +++ b/src/elf_sections.rs @@ -32,25 +32,22 @@ pub struct StringTable(u8); impl StringTable { pub fn section_name(&self, section: &ElfSection) -> &'static str { - unsafe fn strlen(start: *const u8) -> usize { - for i in 0.. { - if *start.offset(i) == 0 { - return i as usize; - } - } - unreachable!() - } use core::{str, slice}; let name_ptr = unsafe { (&self.0 as *const u8).offset(section.name_index as isize) }; - let strlen = unsafe { strlen(name_ptr) }; + let strlen = { + let mut len = 0; + while unsafe { *name_ptr.offset(len) } != 0 { + len += 1; + } + len as usize + }; - unsafe { - str::from_utf8( - slice::from_raw_parts(name_ptr, strlen)).unwrap() - } + str::from_utf8( unsafe { + slice::from_raw_parts(name_ptr, strlen) + }).unwrap() } }