use of java.nio.MappedByteBuffer in project buck by facebook.
the class ElfSymbolTableScrubberStepTest method test.
@Test
public void test() throws IOException {
ProjectWorkspace workspace = TestDataHelper.createProjectWorkspaceForScenario(this, "elf_shared_lib", tmp);
workspace.setUp();
ElfSymbolTableScrubberStep step = ElfSymbolTableScrubberStep.of(new ProjectFilesystem(tmp.getRoot()), tmp.getRoot().getFileSystem().getPath("libfoo.so"), ".dynsym", /* allowMissing */
false);
step.execute(TestExecutionContext.newInstance());
// Verify that the symbol table values and sizes are zero.
try (FileChannel channel = FileChannel.open(step.getFilesystem().resolve(step.getPath()), StandardOpenOption.READ)) {
MappedByteBuffer buffer = channel.map(READ_ONLY, 0, channel.size());
Elf elf = new Elf(buffer);
ElfSection section = elf.getSectionByName(SECTION).orElseThrow(AssertionError::new).getSecond();
ElfSymbolTable table = ElfSymbolTable.parse(elf.header.ei_class, section.body);
Set<Long> addresses = new HashSet<>();
table.entries.forEach(entry -> {
assertTrue(entry.st_value == 0 || addresses.add((entry.st_value)));
assertThat(entry.st_shndx, Matchers.equalTo(entry.st_shndx != 0 ? ElfSymbolTableScrubberStep.STABLE_SECTION : entry.st_shndx));
assertThat(entry.st_size, Matchers.equalTo(entry.st_info.st_type == ElfSymbolTable.Entry.Info.Type.STT_FUNC ? 0 : entry.st_size));
});
}
}
use of java.nio.MappedByteBuffer in project buck by facebook.
the class ElfVerDefTest method test.
@Test
public void test() throws IOException {
try (FileChannel channel = FileChannel.open(workspace.resolve("libfoo.so"))) {
MappedByteBuffer buffer = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size());
Elf elf = new Elf(buffer);
ElfSection stringTable = elf.getSectionByName(".dynstr").get().getSecond();
ElfVerDef verDef = ElfVerDef.parse(elf.header.ei_class, elf.getSectionByName(".gnu.version_d").get().getSecond().body);
assertThat(verDef.entries, Matchers.hasSize(2));
assertThat(stringTable.lookupString(verDef.entries.get(0).getSecond().get(0).vda_name), Matchers.equalTo("libfoo.so"));
assertThat(stringTable.lookupString(verDef.entries.get(1).getSecond().get(0).vda_name), Matchers.equalTo("VERS_1.0"));
}
}
use of java.nio.MappedByteBuffer in project buck by facebook.
the class ElfVerNeedTest method test.
@Test
public void test() throws IOException {
try (FileChannel channel = FileChannel.open(workspace.resolve("libfoo.so"))) {
MappedByteBuffer buffer = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size());
Elf elf = new Elf(buffer);
ElfSection stringTable = elf.getSectionByName(".dynstr").get().getSecond();
ElfVerNeed verNeed = ElfVerNeed.parse(elf.header.ei_class, elf.getSectionByName(".gnu.version_r").get().getSecond().body);
assertThat(verNeed.entries, Matchers.hasSize(1));
assertThat(stringTable.lookupString(verNeed.entries.get(0).getFirst().vn_file), Matchers.equalTo("libc.so.6"));
assertThat(verNeed.entries.get(0).getSecond(), Matchers.hasSize(1));
assertThat(stringTable.lookupString(verNeed.entries.get(0).getSecond().get(0).vna_name), Matchers.equalTo("GLIBC_2.2.5"));
}
}
use of java.nio.MappedByteBuffer in project buck by facebook.
the class ElfTest method le32.
@Test
public void le32() throws IOException {
ProjectWorkspace workspace = TestDataHelper.createProjectWorkspaceForScenario(this, "samples", tmp);
workspace.setUp();
Path elfPath = workspace.resolve(Paths.get("le32.o"));
try (FileChannel channel = FileChannel.open(elfPath)) {
MappedByteBuffer buffer = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size());
Elf elf = new Elf(buffer);
assertEquals(ElfHeader.EIClass.ELFCLASS32, elf.header.ei_class);
assertEquals(ElfHeader.EIData.ELFDATA2LSB, elf.header.ei_data);
assertEquals(9, elf.getNumberOfSections());
assertTrue(elf.getSectionByName(".text").isPresent());
}
}
use of java.nio.MappedByteBuffer in project buck by facebook.
the class ElfTest method sectionTypes.
@Test
public void sectionTypes() throws IOException {
ProjectWorkspace workspace = TestDataHelper.createProjectWorkspaceForScenario(this, "samples", tmp);
workspace.setUp();
Path elfPath = workspace.resolve(Paths.get("section_types.o"));
try (FileChannel channel = FileChannel.open(elfPath)) {
MappedByteBuffer buffer = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size());
Elf elf = new Elf(buffer);
Optional<ElfSection> section;
section = elf.getSectionByName(".text").map(Pair::getSecond);
assertTrue(section.isPresent());
assertEquals(ElfSectionHeader.SHType.SHT_PROGBITS, section.get().header.sh_type);
section = elf.getSectionByName(".bss").map(Pair::getSecond);
assertTrue(section.isPresent());
assertEquals(ElfSectionHeader.SHType.SHT_NOBITS, section.get().header.sh_type);
section = elf.getSectionByName(".strtab").map(Pair::getSecond);
assertTrue(section.isPresent());
assertEquals(ElfSectionHeader.SHType.SHT_STRTAB, section.get().header.sh_type);
section = elf.getSectionByName(".symtab").map(Pair::getSecond);
assertTrue(section.isPresent());
assertEquals(ElfSectionHeader.SHType.SHT_SYMTAB, section.get().header.sh_type);
}
}
Aggregations