Search in sources :

Example 6 with Elf

use of com.facebook.buck.cxx.elf.Elf 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));
        });
    }
}
Also used : ProjectWorkspace(com.facebook.buck.testutil.integration.ProjectWorkspace) ElfSymbolTable(com.facebook.buck.cxx.elf.ElfSymbolTable) MappedByteBuffer(java.nio.MappedByteBuffer) FileChannel(java.nio.channels.FileChannel) ProjectFilesystem(com.facebook.buck.io.ProjectFilesystem) ElfSection(com.facebook.buck.cxx.elf.ElfSection) Elf(com.facebook.buck.cxx.elf.Elf) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 7 with Elf

use of com.facebook.buck.cxx.elf.Elf in project buck by facebook.

the class AbstractElfClearProgramHeadersStep method execute.

@Override
public StepExecutionResult execute(ExecutionContext context) throws IOException {
    try (FileChannel channel = FileChannel.open(getFilesystem().resolve(getPath()), StandardOpenOption.READ, StandardOpenOption.WRITE)) {
        MappedByteBuffer buffer = channel.map(READ_WRITE, 0, channel.size());
        Elf elf = new Elf(buffer);
        Preconditions.checkState(elf.header.e_phoff == (int) elf.header.e_phoff, "program headers are expected to be within 4GB of beginning of file");
        buffer.position((int) elf.header.e_phoff);
        for (int index = 0; index < elf.header.e_phnum * elf.header.e_phentsize; index++) {
            buffer.put((byte) 0);
        }
    }
    return StepExecutionResult.SUCCESS;
}
Also used : MappedByteBuffer(java.nio.MappedByteBuffer) FileChannel(java.nio.channels.FileChannel) Elf(com.facebook.buck.cxx.elf.Elf)

Example 8 with Elf

use of com.facebook.buck.cxx.elf.Elf in project buck by facebook.

the class ElfClearProgramHeadersStepTest method test.

@Test
public void test() throws IOException {
    ProjectWorkspace workspace = TestDataHelper.createProjectWorkspaceForScenario(this, "elf_shared_lib", tmp);
    workspace.setUp();
    ElfClearProgramHeadersStep step = ElfClearProgramHeadersStep.of(new ProjectFilesystem(tmp.getRoot()), tmp.getRoot().getFileSystem().getPath("libfoo.so"));
    step.execute(TestExecutionContext.newInstance());
    // Verify that the program table section is empty.
    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);
        // Program headers are at the beginning of the file, so truncating to `int` should be ok.
        buffer.position((int) elf.header.e_phoff);
        for (int index = 0; index < elf.header.e_phnum * elf.header.e_phentsize; index++) {
            assertThat((int) buffer.get(), Matchers.equalTo(0));
        }
    }
}
Also used : ProjectWorkspace(com.facebook.buck.testutil.integration.ProjectWorkspace) MappedByteBuffer(java.nio.MappedByteBuffer) FileChannel(java.nio.channels.FileChannel) ProjectFilesystem(com.facebook.buck.io.ProjectFilesystem) Elf(com.facebook.buck.cxx.elf.Elf) Test(org.junit.Test)

Example 9 with Elf

use of com.facebook.buck.cxx.elf.Elf in project buck by facebook.

the class ElfExtractSectionsStepTest method test.

@Test
public void test() throws IOException, InterruptedException {
    // Only run if `objcopy` is available on the system.
    Path objcopy = assumeObjcopy();
    // Run the step.
    ProjectWorkspace workspace = TestDataHelper.createProjectWorkspaceForScenario(this, "elf_shared_lib", tmp);
    workspace.setUp();
    ElfExtractSectionsStep step = ElfExtractSectionsStep.of(new ProjectFilesystem(tmp.getRoot()), ImmutableList.of(objcopy.toString()), tmp.getRoot().getFileSystem().getPath("libfoo.so"), tmp.getRoot().getFileSystem().getPath("libfoo.extracted.so"), ImmutableSet.of(".dynamic"));
    step.execute(TestExecutionContext.newInstance());
    // Verify that the program table section is empty.
    try (FileChannel channel = FileChannel.open(step.getFilesystem().resolve(step.getOutput()), StandardOpenOption.READ)) {
        MappedByteBuffer buffer = channel.map(READ_ONLY, 0, channel.size());
        Elf elf = new Elf(buffer);
        List<String> sections = new ArrayList<>();
        for (int index = 0; index < elf.getNumberOfSections(); index++) {
            ElfSection section = elf.getSectionByIndex(index);
            if (section.header.sh_flags != 0) {
                String name = elf.getSectionName(section.header);
                sections.add(name);
            }
        }
        assertThat(sections, Matchers.equalTo(ImmutableList.of(".dynamic")));
    }
}
Also used : Path(java.nio.file.Path) ProjectWorkspace(com.facebook.buck.testutil.integration.ProjectWorkspace) MappedByteBuffer(java.nio.MappedByteBuffer) FileChannel(java.nio.channels.FileChannel) ArrayList(java.util.ArrayList) ProjectFilesystem(com.facebook.buck.io.ProjectFilesystem) ElfSection(com.facebook.buck.cxx.elf.ElfSection) Elf(com.facebook.buck.cxx.elf.Elf) Test(org.junit.Test)

Aggregations

Elf (com.facebook.buck.cxx.elf.Elf)9 MappedByteBuffer (java.nio.MappedByteBuffer)8 FileChannel (java.nio.channels.FileChannel)8 ElfSection (com.facebook.buck.cxx.elf.ElfSection)7 ProjectFilesystem (com.facebook.buck.io.ProjectFilesystem)4 ProjectWorkspace (com.facebook.buck.testutil.integration.ProjectWorkspace)4 Test (org.junit.Test)4 Pair (com.facebook.buck.model.Pair)3 ByteBuffer (java.nio.ByteBuffer)3 ElfDynamicSection (com.facebook.buck.cxx.elf.ElfDynamicSection)2 ElfSymbolTable (com.facebook.buck.cxx.elf.ElfSymbolTable)2 ImmutableMap (com.google.common.collect.ImmutableMap)2 IOException (java.io.IOException)2 Path (java.nio.file.Path)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1