105 lines
4.1 KiB
Plaintext
105 lines
4.1 KiB
Plaintext
# Tests of ELF files: an executable's headers, segments, and sections, its
|
|
# symbols, the machine it is for, the note naming its build, the file
|
|
# describing its segments without holding them, and the checksum of a link.
|
|
import binary::elf;
|
|
import std::testing;
|
|
|
|
|
|
# A word of bytes, least significant byte first.
|
|
def word(bytes: span<u8>, at: u64, size: u64): u64 {
|
|
var made: u64 = 0;
|
|
for k in 0..size {
|
|
made = made | (bytes[at + k] as u64 << (8 * k));
|
|
}
|
|
return made;
|
|
}
|
|
|
|
# Whether a section, by its number, is named so in the table of section names.
|
|
def section_named(file: span<u8>, k: u64, name: strview): bool {
|
|
val headers := word(file, 0x28, 8);
|
|
val names := headers + 64 * word(file, 0x3E, 2);
|
|
val at := word(file, names + 24, 8) + word(file, headers + 64 * k, 4);
|
|
for i in 0..name.length {
|
|
return false if file[at + i] != name[i];
|
|
}
|
|
return file[at + name.length] == 0;
|
|
}
|
|
|
|
# An executable of code and data, the code loaded with the headers.
|
|
def small_image(code: span<u8>): elf::image {
|
|
val headers := elf::headers_size(2, false);
|
|
var made := elf::image::new(0x400000 + headers);
|
|
made.segment(".text", 0x400000, 0, code, code.length, elf::readable | elf::executable, true);
|
|
made.segment(".data", 0x401000, 0x1000, "data", 4, elf::readable | elf::writable, false);
|
|
made.symbols.append(elf::symbol { name = "_start", address = 0x400000 + headers, size = code.length });
|
|
return made;
|
|
}
|
|
|
|
@test def an_executable_holds_its_segments_and_names_them(t: &mut tester): void {
|
|
val code: u8[3] = [0x31, 0xC0, 0xC3];
|
|
var image := small_image(code[:]);
|
|
image.build_id();
|
|
val bytes := image.file();
|
|
val all := bytes.span;
|
|
t.expects(all[0] == 0x7F && all[1] == 'E' as u8 && all[2] == 'L' as u8 && all[3] == 'F' as u8);
|
|
t.expects(all[4] == 2 && all[5] == 1 && word(all, 16, 2) == 2 && word(all, 18, 2) == 62);
|
|
t.expects(word(all, 24, 8) == 0x400000 + 64 + 2 * 56);
|
|
t.expects(word(all, 0x38, 2) == 2);
|
|
|
|
# The first segment holds the headers, then the code just past them.
|
|
val headers := elf::headers_size(2, false);
|
|
t.expects(headers == 64 + 2 * 56);
|
|
t.expects(word(all, 64, 4) == 1 && word(all, 64 + 4, 4) == 5 && word(all, 64 + 32, 8) == headers + 3);
|
|
t.expects(all[headers] == 0x31 && all[headers + 2] == 0xC3);
|
|
t.expects(word(all, 64 + 56 + 8, 8) == 0x1000 && all[0x1000] == 'd' as u8);
|
|
|
|
# The sections: none, each segment's, the note, then the tables.
|
|
val count := word(all, 0x3C, 2);
|
|
t.expects(count == 7 && word(all, 0x3E, 2) == 6);
|
|
val names: strview[7] = ["", ".text", ".data", ".note.gnu.build-id", ".symtab", ".strtab", ".shstrtab"];
|
|
for k in 0..7u {
|
|
t.expects(section_named(all, k, names[k]), names[k]);
|
|
}
|
|
}
|
|
|
|
@test def the_machine_is_the_one_given(t: &mut tester): void {
|
|
val code: u8[1] = [0xC3];
|
|
var image := small_image(code[:]);
|
|
image.machine = 183;
|
|
val bytes := image.file();
|
|
t.expects(word(bytes.span, 18, 2) == 183);
|
|
}
|
|
|
|
@test def the_build_is_named_by_what_it_loads(t: &mut tester): void {
|
|
val code: u8[3] = [0x31, 0xC0, 0xC3];
|
|
var first := small_image(code[:]);
|
|
first.build_id();
|
|
var again := small_image(code[:]);
|
|
again.build_id();
|
|
val other_code: u8[3] = [0x31, 0xC0, 0xC2];
|
|
var other := small_image(other_code[:]);
|
|
other.build_id();
|
|
val note := first.sections[0].data.span;
|
|
t.expects(note.length == 36 && word(note, 0, 4) == 4 && word(note, 4, 4) == elf::build_id_bytes && word(note, 8, 4) == 3);
|
|
t.expects(note == again.sections[0].data.span);
|
|
t.expects(note != other.sections[0].data.span);
|
|
}
|
|
|
|
@test def a_described_file_holds_no_segment(t: &mut tester): void {
|
|
val code: u8[3] = [0x31, 0xC0, 0xC3];
|
|
var image := small_image(code[:]);
|
|
image.described = true;
|
|
val bytes := image.file();
|
|
val all := bytes.span;
|
|
# Its segments are described, holding nothing in the file, as are their sections.
|
|
t.expects(word(all, 64 + 32, 8) == 0 && word(all, 64 + 40, 8) == elf::headers_size(2, false) + 3);
|
|
val headers := word(all, 0x28, 8);
|
|
t.expects(word(all, headers + 64 + 4, 4) == 8);
|
|
t.expects(all.length < 0x1000);
|
|
}
|
|
|
|
@test def links_are_checked_as_zlib_checks_them(t: &mut tester): void {
|
|
t.expects(elf::crc32("123456789") == 0xCBF43926);
|
|
t.expects(elf::crc32("") == 0);
|
|
}
|