Elf file read symbol and symbol address 1 byte difference

  armv7, c++, elf

A few days ago, i looked through the elf symbol table to compare strings to find a my function.
I could find the target string well and I succeeded in getting the starting offset and size.
However, when comparing this offset with the result of objdump, it can be seen that 1 byte is different.
what is wrong?
I’m not good at English well.

os : windows 10
ide : android studio ndk
target arch : armeabi-v7a

result image :
application result : 0x00908b5
excecpted result : 0x00908b4

What I checked :

  1. elf32_sym, elf64_sym checked (no problem)
  2. struct byte padding checked (no problem)
  3. struct, variable initialization checked

enter image description here

This is a clone from a developer’s git.

template <typename ElfHeaderT, typename SectionHeaderT, typename CallbackT>
void read_sections(const void *image, size_t size, const CallbackT &callback)
{
    const ElfHeaderT *ehdr = static_cast<const ElfHeaderT *>(image);
    const SectionHeaderT *shdrs = (const SectionHeaderT *)((const uint8_t *)image + ehdr->e_shoff);
    const SectionHeaderT *strhdr = &shdrs[ehdr->e_shstrndx];
    const char *strtab = static_cast<const char *>(image) + strhdr->sh_offset;

    for (int i = 0; i < ehdr->e_shnum; ++i)
    {
        section s = {0,};

        s.index= i;
        s.name = strtab + shdrs[i].sh_name;
        s.type = shdrs[i].sh_type;
        s.virtual_address = static_cast<ptrdiff_t>(shdrs[i].sh_addr);
        s.file_offset = static_cast<ptrdiff_t>(shdrs[i].sh_offset);
        s.size = static_cast<size_t>(shdrs[i].sh_size);
        s.entry_size = static_cast<size_t>(shdrs[i].sh_entsize);
        s.address_align = static_cast<size_t>(shdrs[i].sh_addralign);
        callback(s);
    }
}


template <typename SymbolEntryT, typename CallbackT>
void read_symbols(const void *image, unsigned int code_section_index, const section &symbols, const char *names,
    const CallbackT &callback)
{
    const size_t total_syms = symbols.size / sizeof(SymbolEntryT);
    const SymbolEntryT *syms_data = (const SymbolEntryT *)((const uint8_t *)image + symbols.file_offset);

    for (size_t i = 0; i < total_syms; ++i)
    {
        symbol s = {0,};
        const SymbolEntryT &sd = syms_data[i];
        const unsigned type = ELF32_ST_TYPE(sd.st_info);

        if (type != STT_FUNC)
            continue;
        if (sd.st_shndx != code_section_index || !sd.st_size)
            continue;
        s.name = names + sd.st_name;
        s.size = static_cast<size_t>(sd.st_size);
        s.virtual_address = static_cast<size_t>(sd.st_value);
        callback(s);
    }
}

Source: Windows Questions C++

LEAVE A COMMENT