Search in sources :

Example 1 with ElfSymbolTable

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

the class AbstractElfSymbolTableScrubberStep 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);
        // Locate the symbol table section.
        Optional<ElfSection> section = elf.getSectionByName(getSection()).map(Pair::getSecond);
        if (!section.isPresent()) {
            if (isAllowMissing()) {
                return StepExecutionResult.SUCCESS;
            } else {
                throw new IOException(String.format("Error parsing ELF file %s: no such section \"%s\"", getPath(), getSection()));
            }
        }
        // Read in and fixup the symbol table then write it back out.
        ElfSymbolTable table = ElfSymbolTable.parse(elf.header.ei_class, section.get().body);
        ElfSymbolTable fixedUpTable = fixUpSymbolTable(table);
        Preconditions.checkState(table.entries.size() == fixedUpTable.entries.size());
        section.get().body.rewind();
        fixedUpTable.write(elf.header.ei_class, section.get().body);
    }
    return StepExecutionResult.SUCCESS;
}
Also used : ElfSymbolTable(com.facebook.buck.cxx.elf.ElfSymbolTable) MappedByteBuffer(java.nio.MappedByteBuffer) FileChannel(java.nio.channels.FileChannel) IOException(java.io.IOException) ElfSection(com.facebook.buck.cxx.elf.ElfSection) Elf(com.facebook.buck.cxx.elf.Elf) Pair(com.facebook.buck.model.Pair)

Example 2 with ElfSymbolTable

use of com.facebook.buck.cxx.elf.ElfSymbolTable 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)

Aggregations

Elf (com.facebook.buck.cxx.elf.Elf)2 ElfSection (com.facebook.buck.cxx.elf.ElfSection)2 ElfSymbolTable (com.facebook.buck.cxx.elf.ElfSymbolTable)2 MappedByteBuffer (java.nio.MappedByteBuffer)2 FileChannel (java.nio.channels.FileChannel)2 ProjectFilesystem (com.facebook.buck.io.ProjectFilesystem)1 Pair (com.facebook.buck.model.Pair)1 ProjectWorkspace (com.facebook.buck.testutil.integration.ProjectWorkspace)1 IOException (java.io.IOException)1 HashSet (java.util.HashSet)1 Test (org.junit.Test)1