| 1 |
use object::{build, elf}; |
| 2 |
|
| 3 |
// Test that offset 0 is supported for SHT_NOBITS sections. |
| 4 |
#[test] |
| 5 |
fn test_nobits_offset() { |
| 6 |
let mut builder = build::elf::Builder::new(object::Endianness::Little, true); |
| 7 |
builder.header.e_type = elf::ET_EXEC; |
| 8 |
builder.header.e_phoff = 0x40; |
| 9 |
|
| 10 |
let section = builder.sections.add(); |
| 11 |
section.name = b".shstrtab"[..].into(); |
| 12 |
section.sh_type = elf::SHT_STRTAB; |
| 13 |
section.data = build::elf::SectionData::SectionString; |
| 14 |
|
| 15 |
let section = builder.sections.add(); |
| 16 |
section.name = b".bss"[..].into(); |
| 17 |
section.sh_type = elf::SHT_NOBITS; |
| 18 |
section.sh_flags = (elf::SHF_ALLOC | elf::SHF_WRITE) as u64; |
| 19 |
section.sh_addr = 0x1000; |
| 20 |
section.sh_offset = 0; |
| 21 |
section.sh_size = 0x1000; |
| 22 |
section.sh_addralign = 16; |
| 23 |
section.data = build::elf::SectionData::UninitializedData(0x1000); |
| 24 |
let section_id = section.id(); |
| 25 |
|
| 26 |
let segment = builder.segments.add(); |
| 27 |
segment.p_type = elf::PT_LOAD; |
| 28 |
segment.p_flags = elf::PF_R | elf::PF_W; |
| 29 |
segment.p_offset = 0x1000; |
| 30 |
segment.p_vaddr = 0x1000; |
| 31 |
segment.p_paddr = 0x1000; |
| 32 |
segment.p_filesz = 0; |
| 33 |
segment.p_memsz = 0x1000; |
| 34 |
segment.p_align = 16; |
| 35 |
segment.sections.push(section_id); |
| 36 |
|
| 37 |
let mut buf = Vec::new(); |
| 38 |
builder.write(&mut buf).unwrap(); |
| 39 |
} |
| 40 |
|
| 41 |
// Test that we can read and write a file with no dynamic string table. |
| 42 |
#[test] |
| 43 |
fn test_no_dynstr() { |
| 44 |
let mut builder = build::elf::Builder::new(object::Endianness::Little, true); |
| 45 |
builder.header.e_type = elf::ET_EXEC; |
| 46 |
builder.header.e_machine = elf::EM_X86_64; |
| 47 |
builder.header.e_phoff = 0x40; |
| 48 |
|
| 49 |
let section = builder.sections.add(); |
| 50 |
section.name = b".shstrtab"[..].into(); |
| 51 |
section.sh_type = elf::SHT_STRTAB; |
| 52 |
section.data = build::elf::SectionData::SectionString; |
| 53 |
|
| 54 |
let section = builder.sections.add(); |
| 55 |
section.name = b".dynsym"[..].into(); |
| 56 |
section.sh_type = elf::SHT_DYNSYM; |
| 57 |
section.sh_flags = elf::SHF_ALLOC as u64; |
| 58 |
section.sh_addralign = 8; |
| 59 |
section.data = build::elf::SectionData::DynamicSymbol; |
| 60 |
let dynsym_id = section.id(); |
| 61 |
|
| 62 |
let section = builder.sections.add(); |
| 63 |
section.name = b".rela.dyn"[..].into(); |
| 64 |
section.sh_type = elf::SHT_RELA; |
| 65 |
section.sh_flags = elf::SHF_ALLOC as u64; |
| 66 |
section.sh_addralign = 8; |
| 67 |
section.data = |
| 68 |
build::elf::SectionData::DynamicRelocation(vec![build::elf::DynamicRelocation { |
| 69 |
r_offset: 0x1000, |
| 70 |
symbol: None, |
| 71 |
r_type: elf::R_X86_64_64, |
| 72 |
r_addend: 0x300, |
| 73 |
}]); |
| 74 |
let rela_id = section.id(); |
| 75 |
|
| 76 |
builder.set_section_sizes(); |
| 77 |
|
| 78 |
let segment = builder.segments.add(); |
| 79 |
segment.p_type = elf::PT_LOAD; |
| 80 |
segment.p_flags = elf::PF_R; |
| 81 |
segment.p_filesz = 0x1000; |
| 82 |
segment.p_memsz = 0x1000; |
| 83 |
segment.p_align = 8; |
| 84 |
segment.append_section(builder.sections.get_mut(dynsym_id)); |
| 85 |
segment.append_section(builder.sections.get_mut(rela_id)); |
| 86 |
|
| 87 |
let mut buf = Vec::new(); |
| 88 |
builder.write(&mut buf).unwrap(); |
| 89 |
|
| 90 |
let builder = build::elf::Builder::read(&*buf).unwrap(); |
| 91 |
assert_eq!(builder.sections.count(), 3); |
| 92 |
assert_eq!(builder.segments.count(), 1); |
| 93 |
for section in &builder.sections { |
| 94 |
match §ion.data { |
| 95 |
build::elf::SectionData::DynamicSymbol => { |
| 96 |
assert_eq!(section.sh_offset, 0x1000); |
| 97 |
} |
| 98 |
build::elf::SectionData::DynamicRelocation(rela) => { |
| 99 |
assert_eq!(section.sh_offset, 0x1018); |
| 100 |
assert_eq!(rela.len(), 1); |
| 101 |
} |
| 102 |
_ => {} |
| 103 |
} |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
#[test] |
| 108 |
fn test_attribute() { |
| 109 |
let mut builder = build::elf::Builder::new(object::Endianness::Little, true); |
| 110 |
builder.header.e_type = elf::ET_EXEC; |
| 111 |
builder.header.e_machine = elf::EM_X86_64; |
| 112 |
builder.header.e_phoff = 0x40; |
| 113 |
|
| 114 |
let section = builder.sections.add(); |
| 115 |
section.name = b".shstrtab"[..].into(); |
| 116 |
section.sh_type = elf::SHT_STRTAB; |
| 117 |
section.data = build::elf::SectionData::SectionString; |
| 118 |
|
| 119 |
let attributes = build::elf::AttributesSection { |
| 120 |
subsections: vec![build::elf::AttributesSubsection { |
| 121 |
vendor: b"GNU"[..].into(), |
| 122 |
subsubsections: vec![ |
| 123 |
(build::elf::AttributesSubsubsection { |
| 124 |
tag: build::elf::AttributeTag::File, |
| 125 |
data: b"123"[..].into(), |
| 126 |
}), |
| 127 |
], |
| 128 |
}], |
| 129 |
}; |
| 130 |
let section = builder.sections.add(); |
| 131 |
section.name = b".gnu.attributes"[..].into(); |
| 132 |
section.sh_type = elf::SHT_GNU_ATTRIBUTES; |
| 133 |
section.sh_addralign = 8; |
| 134 |
section.data = build::elf::SectionData::Attributes(attributes); |
| 135 |
|
| 136 |
let mut buf = Vec::new(); |
| 137 |
builder.write(&mut buf).unwrap(); |
| 138 |
|
| 139 |
let builder = build::elf::Builder::read(&*buf).unwrap(); |
| 140 |
assert_eq!(builder.sections.count(), 2); |
| 141 |
for section in &builder.sections { |
| 142 |
if let build::elf::SectionData::Attributes(attributes) = §ion.data { |
| 143 |
assert_eq!(attributes.subsections.len(), 1); |
| 144 |
assert_eq!(attributes.subsections[0].vendor.as_slice(), b"GNU"); |
| 145 |
assert_eq!(attributes.subsections[0].subsubsections.len(), 1); |
| 146 |
assert_eq!( |
| 147 |
attributes.subsections[0].subsubsections[0].tag, |
| 148 |
build::elf::AttributeTag::File |
| 149 |
); |
| 150 |
assert_eq!( |
| 151 |
attributes.subsections[0].subsubsections[0].data.as_slice(), |
| 152 |
b"123" |
| 153 |
); |
| 154 |
} |
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
#[test] |
| 159 |
fn test_dynsym() { |
| 160 |
let mut builder = build::elf::Builder::new(object::Endianness::Little, true); |
| 161 |
builder.header.e_type = elf::ET_EXEC; |
| 162 |
builder.header.e_machine = elf::EM_X86_64; |
| 163 |
builder.header.e_phoff = 0x40; |
| 164 |
|
| 165 |
let section = builder.sections.add(); |
| 166 |
section.name = b".shstrtab"[..].into(); |
| 167 |
section.sh_type = elf::SHT_STRTAB; |
| 168 |
section.data = build::elf::SectionData::SectionString; |
| 169 |
|
| 170 |
let section = builder.sections.add(); |
| 171 |
section.name = b".text"[..].into(); |
| 172 |
section.sh_type = elf::SHT_PROGBITS; |
| 173 |
section.sh_flags = (elf::SHF_ALLOC | elf::SHF_EXECINSTR) as u64; |
| 174 |
section.sh_addralign = 16; |
| 175 |
section.data = build::elf::SectionData::Data(vec![0xcc; 100].into()); |
| 176 |
let text_id = section.id(); |
| 177 |
|
| 178 |
let section = builder.sections.add(); |
| 179 |
section.name = b".dynsym"[..].into(); |
| 180 |
section.sh_type = elf::SHT_DYNSYM; |
| 181 |
section.sh_flags = elf::SHF_ALLOC as u64; |
| 182 |
section.sh_addralign = 8; |
| 183 |
section.data = build::elf::SectionData::DynamicSymbol; |
| 184 |
let dynsym_id = section.id(); |
| 185 |
|
| 186 |
let section = builder.sections.add(); |
| 187 |
section.name = b".dynstr"[..].into(); |
| 188 |
section.sh_type = elf::SHT_STRTAB; |
| 189 |
section.sh_flags = elf::SHF_ALLOC as u64; |
| 190 |
section.sh_addralign = 1; |
| 191 |
section.data = build::elf::SectionData::DynamicString; |
| 192 |
let dynstr_id = section.id(); |
| 193 |
|
| 194 |
let section = builder.sections.add(); |
| 195 |
section.name = b".gnu.hash"[..].into(); |
| 196 |
section.sh_type = elf::SHT_GNU_HASH; |
| 197 |
section.sh_flags = elf::SHF_ALLOC as u64; |
| 198 |
section.sh_addralign = 8; |
| 199 |
section.data = build::elf::SectionData::GnuHash; |
| 200 |
let gnu_hash_id = section.id(); |
| 201 |
builder.gnu_hash_bloom_shift = 1; |
| 202 |
builder.gnu_hash_bloom_count = 1; |
| 203 |
builder.gnu_hash_bucket_count = 1; |
| 204 |
|
| 205 |
let symbol = builder.dynamic_symbols.add(); |
| 206 |
symbol.name = b"global"[..].into(); |
| 207 |
symbol.set_st_info(elf::STB_GLOBAL, elf::STT_FUNC); |
| 208 |
symbol.section = Some(text_id); |
| 209 |
|
| 210 |
let symbol = builder.dynamic_symbols.add(); |
| 211 |
symbol.name = b"undefined"[..].into(); |
| 212 |
symbol.set_st_info(elf::STB_GLOBAL, elf::STT_NOTYPE); |
| 213 |
|
| 214 |
let symbol = builder.dynamic_symbols.add(); |
| 215 |
symbol.name = b"local"[..].into(); |
| 216 |
symbol.set_st_info(elf::STB_LOCAL, elf::STT_FUNC); |
| 217 |
symbol.section = Some(text_id); |
| 218 |
|
| 219 |
builder.set_section_sizes(); |
| 220 |
|
| 221 |
let segment = builder.segments.add(); |
| 222 |
segment.p_type = elf::PT_LOAD; |
| 223 |
segment.p_flags = elf::PF_R; |
| 224 |
segment.p_filesz = 0x1000; |
| 225 |
segment.p_memsz = 0x1000; |
| 226 |
segment.p_align = 8; |
| 227 |
segment.append_section(builder.sections.get_mut(text_id)); |
| 228 |
segment.append_section(builder.sections.get_mut(dynsym_id)); |
| 229 |
segment.append_section(builder.sections.get_mut(dynstr_id)); |
| 230 |
segment.append_section(builder.sections.get_mut(gnu_hash_id)); |
| 231 |
|
| 232 |
let mut buf = Vec::new(); |
| 233 |
builder.write(&mut buf).unwrap(); |
| 234 |
|
| 235 |
let builder = build::elf::Builder::read(&*buf).unwrap(); |
| 236 |
assert_eq!(builder.sections.count(), 5); |
| 237 |
assert_eq!(builder.dynamic_symbols.count(), 3); |
| 238 |
// Check that the dynamic symbol table sorting handles |
| 239 |
// local and undefined symbols correctly. |
| 240 |
assert_eq!( |
| 241 |
builder |
| 242 |
.dynamic_symbols |
| 243 |
.iter() |
| 244 |
.map(|s| s.name.as_slice()) |
| 245 |
.collect::<Vec<_>>(), |
| 246 |
vec![&b"local"[..], &b"undefined"[..], &b"global"[..]] |
| 247 |
); |
| 248 |
for section in &builder.sections { |
| 249 |
if let build::elf::SectionData::DynamicSymbol = §ion.data { |
| 250 |
// Check that sh_info includes the number of local symbols. |
| 251 |
assert_eq!(section.sh_info, 2); |
| 252 |
} |
| 253 |
} |
| 254 |
} |