summaryrefslogtreecommitdiff
path: root/src/format/elf/format_elf.c
blob: 9f929fb3bbcc2d01c8037287c58d3f5c5f2f9ccd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
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

/* OpenIDA - Outil d'analyse de fichiers binaires
 * format_elf.c - support du format ELF
 *
 * Copyright (C) 2008 Cyrille Bagard
 *
 *  This file is part of OpenIDA.
 *
 *  OpenIDA is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  OpenIDA is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with Foobar.  If not, see <http://www.gnu.org/licenses/>.
 */


#include "elf.h"










#include <ctype.h>
#include <elf.h>
#include <malloc.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>


char *read_section_names(const uint8_t *content, Elf32_Off offset)
{
    char *result;
    Elf32_Shdr section;

    result = NULL;

    memcpy(&section, &content[offset], sizeof(Elf32_Shdr));

    result = (char *)calloc(section.sh_size + 1, sizeof(char));

    memcpy(result, &content[section.sh_offset], section.sh_size);

    return result;

}


bool find_target_section(const uint8_t *content, const char *target, const char *names, Elf32_Off offset, Elf32_Shdr *data)
{
    bool result;
    Elf32_Shdr section;

    result = false;

    memcpy(&section, &content[offset], sizeof(Elf32_Shdr));

    result = (strcmp(target, &names[section.sh_name]) == 0);

    if (result)
    {
        printf("section: %s (0x%08x)\n", &names[section.sh_name], section.sh_addr);
        *data = section;
    }

    return result;

}



bool find_text_data(const uint8_t *content, off_t *offset, off_t *size, uint64_t *voffset)
{
    bool result;
    Elf32_Ehdr header;
    char *names;
    Elf32_Half i;
    Elf32_Shdr data;

    result = false;

    memcpy(&header, content, sizeof(Elf32_Ehdr));

    names = read_section_names(content, header.e_shoff + header.e_shentsize * header.e_shstrndx);
    if (names == NULL)
    {
        fprintf(stderr, "no section header string table\n");
        return NULL;
    }

    for (i = 0; i < header.e_shnum; i++)
    {
        if (i == header.e_shstrndx) continue;

        if (find_target_section(content, ".text", names, header.e_shoff + header.e_shentsize * i, &data))
        {
            printf("Find it !\n");

            *offset = data.sh_offset;
            *size = data.sh_size;
            *voffset = data.sh_addr;

            result = true;

        }

    }

    free(names);

    return result;

}