aboutsummaryrefslogtreecommitdiffstats
path: root/APG-API-Demo
diff options
context:
space:
mode:
Diffstat (limited to 'APG-API-Demo')
0 files changed, 0 insertions, 0 deletions
> 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
/*
 * various helper functions to access elf structures
 */

#include "libelf-private.h"

/* ------------------------------------------------------------------------ */

uint64_t elf_access_unsigned(struct elf_binary * elf, const void *ptr,
                             uint64_t offset, size_t size)
{
    int need_swap = elf_swap(elf);
    const uint8_t *u8;
    const uint16_t *u16;
    const uint32_t *u32;
    const uint64_t *u64;

    switch ( size )
    {
    case 1:
        u8 = ptr + offset;
        return *u8;
    case 2:
        u16 = ptr + offset;
        return need_swap ? bswap_16(*u16) : *u16;
    case 4:
        u32 = ptr + offset;
        return need_swap ? bswap_32(*u32) : *u32;
    case 8:
        u64 = ptr + offset;
        return need_swap ? bswap_64(*u64) : *u64;
    default:
        return 0;
    }
}

int64_t elf_access_signed(struct elf_binary *elf, const void *ptr,
                          uint64_t offset, size_t size)
{
    int need_swap = elf_swap(elf);
    const int8_t *s8;
    const int16_t *s16;
    const int32_t *s32;
    const int64_t *s64;

    switch ( size )
    {
    case 1:
        s8 = ptr + offset;
        return *s8;
    case 2:
        s16 = ptr + offset;
        return need_swap ? bswap_16(*s16) : *s16;
    case 4:
        s32 = ptr + offset;
        return need_swap ? bswap_32(*s32) : *s32;
    case 8:
        s64 = ptr + offset;
        return need_swap ? bswap_64(*s64) : *s64;
    default:
        return 0;
    }
}

uint64_t elf_round_up(struct elf_binary *elf, uint64_t addr)
{
    int elf_round = (elf_64bit(elf) ? 8 : 4) - 1;

    return (addr + elf_round) & ~elf_round;
}

/* ------------------------------------------------------------------------ */

int elf_shdr_count(struct elf_binary *elf)
{
    return elf_uval(elf, elf->ehdr, e_shnum);
}

int elf_phdr_count(struct elf_binary *elf)
{
    return elf_uval(elf, elf->ehdr, e_phnum);
}

const elf_shdr *elf_shdr_by_name(struct elf_binary *elf, const char *name)
{
    uint64_t count = elf_shdr_count(elf);
    const elf_shdr *shdr;
    const char *sname;
    int i;

    for ( i = 0; i < count; i++ )
    {
        shdr = elf_shdr_by_index(elf, i);
        sname = elf_section_name(elf, shdr);
        if ( sname && !strcmp(sname, name) )
            return shdr;
    }
    return NULL;
}

const elf_shdr *elf_shdr_by_index(struct elf_binary *elf, int index)
{
    uint64_t count = elf_shdr_count(elf);
    const void *ptr;

    if ( index >= count )
        return NULL;

    ptr = (elf->image
           + elf_uval(elf, elf->ehdr, e_shoff)
           + elf_uval(elf, elf->ehdr, e_shentsize) * index);
    return ptr;
}

const elf_phdr *elf_phdr_by_index(struct elf_binary *elf, int index)
{
    uint64_t count = elf_uval(elf, elf->ehdr, e_phnum);
    const void *ptr;

    if ( index >= count )
        return NULL;

    ptr = (elf->image
           + elf_uval(elf, elf->ehdr, e_phoff)
           + elf_uval(elf, elf->ehdr, e_phentsize) * index);
    return ptr;
}

const char *elf_section_name(struct elf_binary *elf, const elf_shdr * shdr)
{
    if ( elf->sec_strtab == NULL )
        return "unknown";
    return elf->sec_strtab + elf_uval(elf, shdr, sh_name);
}

const void *elf_section_start(struct elf_binary *elf, const elf_shdr * shdr)
{
    return elf->image + elf_uval(elf, shdr, sh_offset);
}

const void *elf_section_end(struct elf_binary *elf, const elf_shdr * shdr)
{
    return elf->image
        + elf_uval(elf, shdr, sh_offset) + elf_uval(elf, shdr, sh_size);