Skip to content

Commit 582d5e1

Browse files
committed
Change pub fields into functions.
1 parent f002142 commit 582d5e1

File tree

4 files changed

+110
-46
lines changed

4 files changed

+110
-46
lines changed

src/elf_sections.rs

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
pub struct ElfSectionsTag {
55
typ: u32,
66
size: u32,
7-
pub number_of_sections: u32,
7+
number_of_sections: u32,
88
entry_size: u32,
99
shndx: u32, // string table
1010
first_section: ElfSection,
@@ -83,10 +83,10 @@ impl Iterator for ElfSectionIter {
8383
pub struct ElfSection {
8484
name_index: u32,
8585
typ: u32,
86-
pub flags: u32,
87-
pub addr: u32,
86+
flags: u32,
87+
addr: u32,
8888
offset: u32,
89-
pub size: u32,
89+
size: u32,
9090
link: u32,
9191
info: u32,
9292
addralign: u32,
@@ -99,17 +99,41 @@ pub struct ElfSection {
9999
pub struct ElfSection {
100100
name_index: u32,
101101
typ: u32,
102-
pub flags: u64,
103-
pub addr: u64,
102+
flags: u64,
103+
addr: u64,
104104
offset: u64,
105-
pub size: u64,
105+
size: u64,
106106
link: u32,
107107
info: u32,
108108
addralign: u64,
109109
entry_size: u64,
110110
}
111111

112112
impl ElfSection {
113+
pub fn section_type(&self) -> ElfSectionType {
114+
match self.typ {
115+
0 => ElfSectionType::Unused,
116+
1 => ElfSectionType::ProgramSection,
117+
2 => ElfSectionType::LinkerSymbolTable,
118+
3 => ElfSectionType::StringTable,
119+
4 => ElfSectionType::RelaRelocation,
120+
5 => ElfSectionType::SymbolHashTable,
121+
6 => ElfSectionType::DynamicLinkingTable,
122+
7 => ElfSectionType::Note,
123+
8 => ElfSectionType::Uninitialized,
124+
9 => ElfSectionType::RelRelocation,
125+
10 => ElfSectionType::Reserved,
126+
11 => ElfSectionType::DynamicLoaderSymbolTable,
127+
0x6000_0000...0x6FFF_FFFF => ElfSectionType::EnvironmentSpecific,
128+
0x7000_0000...0x7FFF_FFFF => ElfSectionType::ProcessorSpecific,
129+
_ => panic!(),
130+
}
131+
}
132+
133+
pub fn section_type_raw(&self) -> u32 {
134+
self.typ
135+
}
136+
113137
pub fn start_address(&self) -> usize {
114138
self.addr as usize
115139
}
@@ -118,6 +142,10 @@ impl ElfSection {
118142
(self.addr + self.size) as usize
119143
}
120144

145+
pub fn size(&self) -> usize {
146+
self.size as usize
147+
}
148+
121149
pub fn flags(&self) -> ElfSectionFlags {
122150
ElfSectionFlags::from_bits_truncate(self.flags)
123151
}
@@ -127,6 +155,7 @@ impl ElfSection {
127155
}
128156
}
129157

158+
#[derive(PartialEq, Eq, Debug, Copy, Clone)]
130159
#[repr(u32)]
131160
pub enum ElfSectionType {
132161
Unused = 0,
@@ -141,8 +170,8 @@ pub enum ElfSectionType {
141170
RelRelocation = 9,
142171
Reserved = 10,
143172
DynamicLoaderSymbolTable = 11,
144-
// plus environment-specific use from 0x60000000 to 0x6FFFFFFF
145-
// plus processor-specific use from 0x70000000 to 0x7FFFFFFF
173+
EnvironmentSpecific = 0x6000_0000,
174+
ProcessorSpecific = 0x7000_0000,
146175
}
147176

148177
#[cfg(feature = "elf32")]

src/lib.rs

Lines changed: 51 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ pub unsafe fn load(address: usize) -> &'static BootInformation {
3232

3333
#[repr(C)]
3434
pub struct BootInformation {
35-
pub total_size: u32,
35+
total_size: u32,
3636
_reserved: u32,
3737
first_tag: Tag,
3838
}
@@ -43,27 +43,31 @@ impl BootInformation {
4343
}
4444

4545
pub fn end_address(&self) -> usize {
46-
self.start_address() + self.total_size as usize
46+
self.start_address() + self.total_size()
47+
}
48+
49+
pub fn total_size(&self) -> usize {
50+
self.total_size as usize
4751
}
4852

4953
pub fn elf_sections_tag(&self) -> Option<&'static ElfSectionsTag> {
50-
self.get_tag(9).map(|tag| unsafe{&*(tag as *const Tag as *const ElfSectionsTag)})
54+
self.get_tag(9).map(|tag| unsafe { &*(tag as *const Tag as *const ElfSectionsTag) })
5155
}
5256

5357
pub fn memory_map_tag(&self) -> Option<&'static MemoryMapTag> {
54-
self.get_tag(6).map(|tag| unsafe{&*(tag as *const Tag as *const MemoryMapTag)})
58+
self.get_tag(6).map(|tag| unsafe { &*(tag as *const Tag as *const MemoryMapTag) })
5559
}
5660

5761
pub fn module_tags(&self) -> ModuleIter {
58-
ModuleIter{ iter: self.tags() }
62+
module::module_iter(self.tags())
5963
}
6064

6165
pub fn boot_loader_name_tag(&self) -> Option<&'static BootLoaderNameTag> {
62-
self.get_tag(2).map(|tag| unsafe{&*(tag as *const Tag as *const BootLoaderNameTag)})
66+
self.get_tag(2).map(|tag| unsafe { &*(tag as *const Tag as *const BootLoaderNameTag) })
6367
}
6468

6569
pub fn command_line_tag(&self) -> Option<&'static CommandLineTag> {
66-
self.get_tag(1).map(|tag| unsafe{&*(tag as *const Tag as *const CommandLineTag)})
70+
self.get_tag(1).map(|tag| unsafe { &*(tag as *const Tag as *const CommandLineTag) })
6771
}
6872

6973
fn has_valid_end_tag(&self) -> bool {
@@ -81,7 +85,7 @@ impl BootInformation {
8185
}
8286

8387
fn tags(&self) -> TagIter {
84-
TagIter{current: &self.first_tag as *const _}
88+
TagIter { current: &self.first_tag as *const _ }
8589
}
8690
}
8791

@@ -90,7 +94,7 @@ impl fmt::Debug for BootInformation {
9094
writeln!(f, "multiboot information")?;
9195

9296
writeln!(f, "S: {:#010X}, E: {:#010X}, L: {:#010X}",
93-
self.start_address(), self.end_address(), self.total_size)?;
97+
self.start_address(), self.end_address(), self.total_size())?;
9498

9599
if let Some(boot_loader_name_tag) = self.boot_loader_name_tag() {
96100
writeln!(f, "boot loader name: {}", boot_loader_name_tag.name())?;
@@ -104,7 +108,7 @@ impl fmt::Debug for BootInformation {
104108
writeln!(f, "memory areas:")?;
105109
for area in memory_map_tag.memory_areas() {
106110
writeln!(f, " S: {:#010X}, E: {:#010X}, L: {:#010X}",
107-
area.base_addr, area.base_addr + area.length, area.length)?;
111+
area.start_address(), area.end_address(), area.size())?;
108112
}
109113
}
110114

@@ -113,7 +117,8 @@ impl fmt::Debug for BootInformation {
113117
writeln!(f, "kernel sections:")?;
114118
for s in elf_sections_tag.sections() {
115119
writeln!(f, " name: {:15}, S: {:#08X}, E: {:#08X}, L: {:#08X}, F: {:#04X}",
116-
string_table.section_name(s), s.addr, s.addr + s.size, s.size, s.flags)?;
120+
string_table.section_name(s), s.start_address(),
121+
s.start_address() + s.size(), s.size(), s.flags().bits())?;
117122
}
118123
}
119124

@@ -130,6 +135,8 @@ impl fmt::Debug for BootInformation {
130135
#[cfg(test)]
131136
mod tests {
132137
use super::load;
138+
use super::{ElfSectionFlags, ELF_SECTION_EXECUTABLE, ELF_SECTION_ALLOCATED, ELF_SECTION_WRITABLE};
139+
use super::ElfSectionType;
133140

134141
#[test]
135142
fn no_tags() {
@@ -143,7 +150,7 @@ mod tests {
143150
let bi = unsafe { load(addr) };
144151
assert_eq!(addr, bi.start_address());
145152
assert_eq!(addr + bytes.len(), bi.end_address());
146-
assert_eq!(bytes.len(), bi.total_size as usize);
153+
assert_eq!(bytes.len(), bi.total_size());
147154
assert!(bi.elf_sections_tag().is_none());
148155
assert!(bi.memory_map_tag().is_none());
149156
assert!(bi.module_tags().next().is_none());
@@ -165,7 +172,7 @@ mod tests {
165172
let bi = unsafe { load(addr) };
166173
assert_eq!(addr, bi.start_address());
167174
assert_eq!(addr + bytes.len(), bi.end_address());
168-
assert_eq!(bytes.len(), bi.total_size as usize);
175+
assert_eq!(bytes.len(), bi.total_size());
169176
assert!(bi.elf_sections_tag().is_none());
170177
assert!(bi.memory_map_tag().is_none());
171178
assert!(bi.module_tags().next().is_none());
@@ -187,7 +194,7 @@ mod tests {
187194
let bi = unsafe { load(addr) };
188195
assert_eq!(addr, bi.start_address());
189196
assert_eq!(addr + bytes.len(), bi.end_address());
190-
assert_eq!(bytes.len(), bi.total_size as usize);
197+
assert_eq!(bytes.len(), bi.total_size());
191198
assert!(bi.elf_sections_tag().is_none());
192199
assert!(bi.memory_map_tag().is_none());
193200
assert!(bi.module_tags().next().is_none());
@@ -211,14 +218,15 @@ mod tests {
211218
let bi = unsafe { load(addr) };
212219
assert_eq!(addr, bi.start_address());
213220
assert_eq!(addr + bytes.len(), bi.end_address());
214-
assert_eq!(bytes.len(), bi.total_size as usize);
221+
assert_eq!(bytes.len(), bi.total_size());
215222
assert!(bi.elf_sections_tag().is_none());
216223
assert!(bi.memory_map_tag().is_none());
217224
assert!(bi.module_tags().next().is_none());
218225
assert_eq!("name", bi.boot_loader_name_tag().unwrap().name());
219226
assert!(bi.command_line_tag().is_none());
220227
}
221228

229+
#[cfg(not(feature = "elf32"))]
222230
#[test]
223231
fn grub2() {
224232
let mut bytes: [u8; 960] = [
@@ -490,7 +498,7 @@ mod tests {
490498
let bi = unsafe { load(addr) };
491499
assert_eq!(addr, bi.start_address());
492500
assert_eq!(addr + bytes.len(), bi.end_address());
493-
assert_eq!(bytes.len(), bi.total_size as usize);
501+
assert_eq!(bytes.len(), bi.total_size() as usize);
494502
let es = bi.elf_sections_tag().unwrap();
495503
let st = es.string_table();
496504
assert_eq!(string_addr, st as *const _ as u64);
@@ -499,52 +507,61 @@ mod tests {
499507
assert_eq!(".rodata", st.section_name(s1));
500508
assert_eq!(0xFFFF_8000_0010_0000, s1.start_address());
501509
assert_eq!(0xFFFF_8000_0010_3000, s1.end_address());
502-
assert_eq!(0x0000_0000_0000_3000, s1.size);
503-
assert_eq!(2, s1.flags);
510+
assert_eq!(0x0000_0000_0000_3000, s1.size());
511+
assert_eq!(ELF_SECTION_ALLOCATED, s1.flags());
512+
assert_eq!(ElfSectionType::ProgramSection, s1.section_type());
504513
let s2 = s.next().unwrap();
505514
assert_eq!(".text", st.section_name(s2));
506515
assert_eq!(0xFFFF_8000_0010_3000, s2.start_address());
507516
assert_eq!(0xFFFF_8000_0010_C000, s2.end_address());
508-
assert_eq!(0x0000_0000_0000_9000, s2.size);
509-
assert_eq!(6, s2.flags);
517+
assert_eq!(0x0000_0000_0000_9000, s2.size());
518+
assert_eq!(ELF_SECTION_EXECUTABLE | ELF_SECTION_ALLOCATED, s2.flags());
519+
assert_eq!(ElfSectionType::ProgramSection, s2.section_type());
510520
let s3 = s.next().unwrap();
511521
assert_eq!(".data", st.section_name(s3));
512522
assert_eq!(0xFFFF_8000_0010_C000, s3.start_address());
513523
assert_eq!(0xFFFF_8000_0010_E000, s3.end_address());
514-
assert_eq!(0x0000_0000_0000_2000, s3.size);
515-
assert_eq!(3, s3.flags);
524+
assert_eq!(0x0000_0000_0000_2000, s3.size());
525+
assert_eq!(ELF_SECTION_ALLOCATED | ELF_SECTION_WRITABLE, s3.flags());
526+
assert_eq!(ElfSectionType::ProgramSection, s3.section_type());
516527
let s4 = s.next().unwrap();
517528
assert_eq!(".bss", st.section_name(s4));
518529
assert_eq!(0xFFFF_8000_0010_E000, s4.start_address());
519530
assert_eq!(0xFFFF_8000_0011_3000, s4.end_address());
520-
assert_eq!(0x0000_0000_0000_5000, s4.size);
521-
assert_eq!(3, s4.flags);
531+
assert_eq!(0x0000_0000_0000_5000, s4.size());
532+
assert_eq!(ELF_SECTION_ALLOCATED | ELF_SECTION_WRITABLE, s4.flags());
533+
assert_eq!(ElfSectionType::Uninitialized, s4.section_type());
522534
let s5 = s.next().unwrap();
523535
assert_eq!(".data.rel.ro", st.section_name(s5));
524536
assert_eq!(0xFFFF_8000_0011_3000, s5.start_address());
525537
assert_eq!(0xFFFF_8000_0011_3000, s5.end_address());
526-
assert_eq!(0x0000_0000_0000_0000, s5.size);
527-
assert_eq!(3, s5.flags);
538+
assert_eq!(0x0000_0000_0000_0000, s5.size());
539+
assert_eq!(ELF_SECTION_ALLOCATED | ELF_SECTION_WRITABLE, s5.flags());
540+
assert_eq!(ElfSectionType::ProgramSection, s5.section_type());
528541
let s6 = s.next().unwrap();
529542
assert_eq!(".symtab", st.section_name(s6));
530543
assert_eq!(0x0000_0000_0011_3000, s6.start_address());
531544
assert_eq!(0x0000_0000_0011_5BE0, s6.end_address());
532-
assert_eq!(0x0000_0000_0000_2BE0, s6.size);
533-
assert_eq!(0, s6.flags);
545+
assert_eq!(0x0000_0000_0000_2BE0, s6.size());
546+
assert_eq!(ElfSectionFlags::empty(), s6.flags());
547+
assert_eq!(ElfSectionType::LinkerSymbolTable, s6.section_type());
534548
let s7 = s.next().unwrap();
535549
assert_eq!(".strtab", st.section_name(s7));
536550
assert_eq!(0x0000_0000_0011_5BE0, s7.start_address());
537551
assert_eq!(0x0000_0000_0011_9371, s7.end_address());
538-
assert_eq!(0x0000_0000_0000_3791, s7.size);
539-
assert_eq!(0, s7.flags);
552+
assert_eq!(0x0000_0000_0000_3791, s7.size());
553+
assert_eq!(ElfSectionFlags::empty(), s7.flags());
554+
assert_eq!(ElfSectionType::StringTable, s7.section_type());
540555
assert!(s.next().is_none());
541556
let mut mm = bi.memory_map_tag().unwrap().memory_areas();
542557
let mm1 = mm.next().unwrap();
543-
assert_eq!(0x00000000, mm1.base_addr);
544-
assert_eq!(0x009_FC00, mm1.length);
558+
assert_eq!(0x00000000, mm1.start_address());
559+
assert_eq!(0x009_FC00, mm1.end_address());
560+
assert_eq!(0x009_FC00, mm1.size());
545561
let mm2 = mm.next().unwrap();
546-
assert_eq!(0x010_0000, mm2.base_addr);
547-
assert_eq!(0x7EE_0000, mm2.length);
562+
assert_eq!(0x010_0000, mm2.start_address());
563+
assert_eq!(0x7FE_0000, mm2.end_address());
564+
assert_eq!(0x7EE_0000, mm2.size());
548565
assert!(mm.next().is_none());
549566
assert!(bi.module_tags().next().is_none());
550567
assert_eq!("GRUB 2.02~beta3-5", bi.boot_loader_name_tag().unwrap().name());

src/memory_map.rs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,26 @@ impl MemoryMapTag {
2424
#[derive(Debug)]
2525
#[repr(C)]
2626
pub struct MemoryArea {
27-
pub base_addr: u64,
28-
pub length: u64,
27+
base_addr: u64,
28+
length: u64,
2929
typ: u32,
3030
_reserved: u32,
3131
}
3232

33+
impl MemoryArea {
34+
pub fn start_address(&self) -> usize {
35+
self.base_addr as usize
36+
}
37+
38+
pub fn end_address(&self) -> usize {
39+
(self.base_addr + self.length) as usize
40+
}
41+
42+
pub fn size(&self) -> usize {
43+
self.length as usize
44+
}
45+
}
46+
3347
#[derive(Clone, Debug)]
3448
pub struct MemoryAreaIter {
3549
current_area: *const MemoryArea,

src/module.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,12 @@ impl ModuleTag {
3232
}
3333
}
3434

35+
pub fn module_iter(iter: TagIter) -> ModuleIter {
36+
ModuleIter { iter: iter }
37+
}
38+
3539
pub struct ModuleIter {
36-
pub iter: TagIter,
40+
iter: TagIter,
3741
}
3842

3943
impl Iterator for ModuleIter {

0 commit comments

Comments
 (0)