1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
|
use object::read::elf::{FileHeader, SectionHeader};
use object::read::{Object, ObjectSection, ObjectSymbol};
use object::{
elf, read, write, Architecture, BinaryFormat, Endianness, LittleEndian, SectionIndex,
SectionKind, SymbolFlags, SymbolKind, SymbolScope, SymbolSection, U32,
};
use std::io::Write;
#[test]
fn symtab_shndx() {
let mut object =
write::Object::new(BinaryFormat::Elf, Architecture::X86_64, Endianness::Little);
for i in 0..0x10000 {
let name = format!("func{}", i).into_bytes();
let section = object.add_subsection(write::StandardSection::Text, &name);
let offset = object.append_section_data(section, &[0xcc], 1);
object.add_symbol(write::Symbol {
name,
value: offset,
size: 1,
kind: SymbolKind::Text,
scope: SymbolScope::Linkage,
weak: false,
section: write::SymbolSection::Section(section),
flags: SymbolFlags::None,
});
}
let bytes = object.write().unwrap();
//std::fs::write(&"symtab_shndx.o", &bytes).unwrap();
let object = read::File::parse(&*bytes).unwrap();
assert_eq!(object.format(), BinaryFormat::Elf);
assert_eq!(object.architecture(), Architecture::X86_64);
for symbol in object.symbols() {
assert_eq!(
symbol.section(),
SymbolSection::Section(SectionIndex(symbol.index().0))
);
}
}
#[test]
fn empty_symtab() {
let object = write::Object::new(BinaryFormat::Elf, Architecture::X86_64, Endianness::Little);
let bytes = object.write().unwrap();
let object = read::File::parse(&*bytes).unwrap();
assert_eq!(object.format(), BinaryFormat::Elf);
assert_eq!(object.architecture(), Architecture::X86_64);
let symtab = object.section_by_name(".symtab").unwrap();
assert_eq!(symtab.size(), 24);
let strtab = object.section_by_name(".strtab").unwrap();
assert_eq!(strtab.size(), 1);
}
#[test]
fn aligned_sections() {
let mut object =
write::Object::new(BinaryFormat::Elf, Architecture::X86_64, Endianness::Little);
let text_section_id = object.add_section(vec![], b".text".to_vec(), SectionKind::Text);
let text_section = object.section_mut(text_section_id);
text_section.set_data(&[][..], 4096);
let data_section_id = object.add_section(vec![], b".data".to_vec(), SectionKind::Data);
let data_section = object.section_mut(data_section_id);
data_section.set_data(&b"1234"[..], 16);
let bytes = object.write().unwrap();
let object = read::File::parse(&*bytes).unwrap();
assert_eq!(object.format(), BinaryFormat::Elf);
assert_eq!(object.architecture(), Architecture::X86_64);
let mut sections = object.sections();
let section = sections.next().unwrap();
assert_eq!(section.name(), Ok(".text"));
assert_eq!(section.file_range(), Some((4096, 0)));
let section = sections.next().unwrap();
assert_eq!(section.name(), Ok(".data"));
assert_eq!(section.file_range(), Some((4096, 4)));
}
#[cfg(feature = "compression")]
#[test]
fn compression_zlib() {
use object::read::ObjectSection;
use object::LittleEndian as LE;
let data = b"test data data data";
let len = data.len() as u64;
let mut ch = object::elf::CompressionHeader64::<LE>::default();
ch.ch_type.set(LE, object::elf::ELFCOMPRESS_ZLIB);
ch.ch_size.set(LE, len);
ch.ch_addralign.set(LE, 1);
let mut buf = Vec::new();
buf.write_all(object::bytes_of(&ch)).unwrap();
let mut encoder = flate2::write::ZlibEncoder::new(buf, flate2::Compression::default());
encoder.write_all(data).unwrap();
let compressed = encoder.finish().unwrap();
let mut object =
write::Object::new(BinaryFormat::Elf, Architecture::X86_64, Endianness::Little);
let section = object.add_section(
Vec::new(),
b".debug_info".to_vec(),
object::SectionKind::Other,
);
object.section_mut(section).set_data(compressed, 1);
object.section_mut(section).flags = object::SectionFlags::Elf {
sh_flags: object::elf::SHF_COMPRESSED.into(),
};
let bytes = object.write().unwrap();
//std::fs::write(&"compression.o", &bytes).unwrap();
let object = read::File::parse(&*bytes).unwrap();
assert_eq!(object.format(), BinaryFormat::Elf);
assert_eq!(object.architecture(), Architecture::X86_64);
let section = object.section_by_name(".debug_info").unwrap();
let uncompressed = section.uncompressed_data().unwrap();
assert_eq!(data, &*uncompressed);
}
#[cfg(feature = "compression")]
#[test]
fn compression_gnu() {
use object::read::ObjectSection;
use std::io::Write;
let data = b"test data data data";
let len = data.len() as u32;
let mut buf = Vec::new();
buf.write_all(b"ZLIB\0\0\0\0").unwrap();
buf.write_all(&len.to_be_bytes()).unwrap();
let mut encoder = flate2::write::ZlibEncoder::new(buf, flate2::Compression::default());
encoder.write_all(data).unwrap();
let compressed = encoder.finish().unwrap();
let mut object =
write::Object::new(BinaryFormat::Elf, Architecture::X86_64, Endianness::Little);
let section = object.add_section(
Vec::new(),
b".zdebug_info".to_vec(),
object::SectionKind::Other,
);
object.section_mut(section).set_data(compressed, 1);
let bytes = object.write().unwrap();
//std::fs::write(&"compression.o", &bytes).unwrap();
let object = read::File::parse(&*bytes).unwrap();
assert_eq!(object.format(), BinaryFormat::Elf);
assert_eq!(object.architecture(), Architecture::X86_64);
let section = object.section_by_name(".zdebug_info").unwrap();
let uncompressed = section.uncompressed_data().unwrap();
assert_eq!(data, &*uncompressed);
}
#[test]
fn note() {
let endian = Endianness::Little;
let mut object = write::Object::new(BinaryFormat::Elf, Architecture::X86_64, endian);
// Add note section with align = 4.
let mut buffer = Vec::new();
buffer
.write_all(object::bytes_of(&elf::NoteHeader32 {
n_namesz: U32::new(endian, 6),
n_descsz: U32::new(endian, 11),
n_type: U32::new(endian, 1),
}))
.unwrap();
buffer.write_all(b"name1\0\0\0").unwrap();
buffer.write_all(b"descriptor\0\0").unwrap();
buffer
.write_all(object::bytes_of(&elf::NoteHeader32 {
n_namesz: U32::new(endian, 6),
n_descsz: U32::new(endian, 11),
n_type: U32::new(endian, 2),
}))
.unwrap();
buffer.write_all(b"name2\0\0\0").unwrap();
buffer.write_all(b"descriptor\0\0").unwrap();
let section = object.add_section(Vec::new(), b".note4".to_vec(), SectionKind::Note);
object.section_mut(section).set_data(buffer, 4);
// Add note section with align = 8.
let mut buffer = Vec::new();
buffer
.write_all(object::bytes_of(&elf::NoteHeader32 {
n_namesz: U32::new(endian, 6),
n_descsz: U32::new(endian, 11),
n_type: U32::new(endian, 1),
}))
.unwrap();
buffer.write_all(b"name1\0\0\0\0\0\0\0").unwrap();
buffer.write_all(b"descriptor\0\0\0\0\0\0").unwrap();
buffer
.write_all(object::bytes_of(&elf::NoteHeader32 {
n_namesz: U32::new(endian, 4),
n_descsz: U32::new(endian, 11),
n_type: U32::new(endian, 2),
}))
.unwrap();
buffer.write_all(b"abc\0").unwrap();
buffer.write_all(b"descriptor\0\0\0\0\0\0").unwrap();
let section = object.add_section(Vec::new(), b".note8".to_vec(), SectionKind::Note);
object.section_mut(section).set_data(buffer, 8);
let bytes = &*object.write().unwrap();
//std::fs::write(&"note.o", &bytes).unwrap();
let header = elf::FileHeader64::parse(bytes).unwrap();
let endian: LittleEndian = header.endian().unwrap();
let sections = header.sections(endian, bytes).unwrap();
let section = sections.section(SectionIndex(1)).unwrap();
assert_eq!(sections.section_name(endian, section).unwrap(), b".note4");
assert_eq!(section.sh_addralign(endian), 4);
let mut notes = section.notes(endian, bytes).unwrap().unwrap();
let note = notes.next().unwrap().unwrap();
assert_eq!(note.name(), b"name1");
assert_eq!(note.desc(), b"descriptor\0");
assert_eq!(note.n_type(endian), 1);
let note = notes.next().unwrap().unwrap();
assert_eq!(note.name(), b"name2");
assert_eq!(note.desc(), b"descriptor\0");
assert_eq!(note.n_type(endian), 2);
assert!(notes.next().unwrap().is_none());
let section = sections.section(SectionIndex(2)).unwrap();
assert_eq!(sections.section_name(endian, section).unwrap(), b".note8");
assert_eq!(section.sh_addralign(endian), 8);
let mut notes = section.notes(endian, bytes).unwrap().unwrap();
let note = notes.next().unwrap().unwrap();
assert_eq!(note.name(), b"name1");
assert_eq!(note.desc(), b"descriptor\0");
assert_eq!(note.n_type(endian), 1);
let note = notes.next().unwrap().unwrap();
assert_eq!(note.name(), b"abc");
assert_eq!(note.desc(), b"descriptor\0");
assert_eq!(note.n_type(endian), 2);
assert!(notes.next().unwrap().is_none());
}
#[test]
fn gnu_property() {
gnu_property_inner::<elf::FileHeader32<Endianness>>(Architecture::I386);
gnu_property_inner::<elf::FileHeader64<Endianness>>(Architecture::X86_64);
}
fn gnu_property_inner<Elf: FileHeader<Endian = Endianness>>(architecture: Architecture) {
let endian = Endianness::Little;
let mut object = write::Object::new(BinaryFormat::Elf, architecture, endian);
object.add_elf_gnu_property_u32(
elf::GNU_PROPERTY_X86_FEATURE_1_AND,
elf::GNU_PROPERTY_X86_FEATURE_1_IBT | elf::GNU_PROPERTY_X86_FEATURE_1_SHSTK,
);
let bytes = &*object.write().unwrap();
//std::fs::write(&"note.o", &bytes).unwrap();
let header = Elf::parse(bytes).unwrap();
assert_eq!(header.endian().unwrap(), endian);
let sections = header.sections(endian, bytes).unwrap();
let section = sections.section(SectionIndex(1)).unwrap();
assert_eq!(
sections.section_name(endian, section).unwrap(),
b".note.gnu.property"
);
assert_eq!(section.sh_flags(endian).into(), u64::from(elf::SHF_ALLOC));
let mut notes = section.notes(endian, bytes).unwrap().unwrap();
let note = notes.next().unwrap().unwrap();
let mut props = note.gnu_properties(endian).unwrap();
let prop = props.next().unwrap().unwrap();
assert_eq!(prop.pr_type(), elf::GNU_PROPERTY_X86_FEATURE_1_AND);
assert_eq!(
prop.data_u32(endian).unwrap(),
elf::GNU_PROPERTY_X86_FEATURE_1_IBT | elf::GNU_PROPERTY_X86_FEATURE_1_SHSTK
);
assert!(props.next().unwrap().is_none());
assert!(notes.next().unwrap().is_none());
}
|