summaryrefslogtreecommitdiff
path: root/tools/ramscrgen/elf.cpp
blob: 7e78704b8c1186c2912faf03cceeb784f115521f (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
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
#include <cstdio>
#include <cstring>
#include <cstdint>
#include <map>
#include <vector>
#include <string>
#include "ramscrgen.h"
#include "elf.h"

#define SHN_COMMON 0xFFF2

static std::string s_elfPath;
static std::string s_archiveFilePath;
static std::string s_archiveObjectPath;

static FILE *s_file;

static std::uint32_t s_sectionHeaderOffset;
static int s_sectionHeaderEntrySize;
static int s_sectionCount;
static int s_shstrtabIndex;

static std::uint32_t s_symtabOffset;
static std::uint32_t s_strtabOffset;

static std::uint32_t s_symbolCount;
static std::uint32_t s_elfFileOffset;

struct Symbol
{
    std::uint32_t nameOffset;
    std::uint32_t size;
};

static void Seek(long offset)
{
    if (std::fseek(s_file, s_elfFileOffset + offset, SEEK_SET) != 0)
        FATAL_ERROR("error: failed to seek to %ld in \"%s\"", offset, s_elfPath.c_str());
}

static void Skip(long offset)
{
    if (std::fseek(s_file, offset, SEEK_CUR) != 0)
        FATAL_ERROR("error: failed to skip %ld bytes in \"%s\"", offset, s_elfPath.c_str());
}

static std::uint32_t ReadInt8()
{
    int c = std::fgetc(s_file);

    if (c < 0)
        FATAL_ERROR("error: unexpected EOF when reading ELF file \"%s\"\n", s_elfPath.c_str());

    return c;
}

static std::uint32_t ReadInt16()
{
    std::uint32_t val = 0;
    val |= ReadInt8();
    val |= ReadInt8() << 8;
    return val;
}

static std::uint32_t ReadInt32()
{
    std::uint32_t val = 0;
    val |= ReadInt8();
    val |= ReadInt8() << 8;
    val |= ReadInt8() << 16;
    val |= ReadInt8() << 24;
    return val;
}

static std::string ReadString()
{
    std::string s;
    char c;

    while ((c = ReadInt8()) != 0)
        s += c;

    return s;
}

static void VerifyElfIdent()
{
    char expectedMagic[4] = { 0x7F, 'E', 'L', 'F' };
    char magic[4];

    if (std::fread(magic, 4, 1, s_file) != 1)
        FATAL_ERROR("error: failed to read ELF magic from \"%s\"\n", s_elfPath.c_str());

    if (std::memcmp(magic, expectedMagic, 4) != 0)
        FATAL_ERROR("error: ELF magic did not match in \"%s\"\n", s_elfPath.c_str());

    if (std::fgetc(s_file) != 1)
        FATAL_ERROR("error: \"%s\" not 32-bit ELF\n", s_elfPath.c_str());

    if (std::fgetc(s_file) != 1)
        FATAL_ERROR("error: \"%s\" not little-endian ELF\n", s_elfPath.c_str());
}

static void VerifyAr()
{
    char expectedMagic[8] = {'!', '<', 'a', 'r', 'c', 'h', '>', '\n'};
    char magic[8];

    if (std::fread(magic, 8, 1, s_file) != 1)
        FATAL_ERROR("error: failed to read AR magic from \"%s\"\n", s_archiveFilePath.c_str());

    if (std::memcmp(magic, expectedMagic, 8) != 0)
        FATAL_ERROR("error: AR magic did not match in \"%s\"\n", s_archiveFilePath.c_str());
}

static void ReadElfHeader()
{
    Seek(0x20);
    s_sectionHeaderOffset = ReadInt32();
    Seek(0x2E);
    s_sectionHeaderEntrySize = ReadInt16();
    s_sectionCount = ReadInt16();
    s_shstrtabIndex = ReadInt16();
}

static void FindArObj()
{
    char file_ident[17] = {0};
    char filesize_s[11] = {0};
    char expectedEndMagic[2] = { 0x60, 0x0a };
    char end_magic[2];
    std::size_t filesize;

    Seek(8);
    while (!std::feof(s_file)) {
        if (std::fread(file_ident, 16, 1, s_file) != 1)
            FATAL_ERROR("error: failed to read file ident in \"%s\"\n", s_archiveFilePath.c_str());
        Skip(32);
        if (std::fread(filesize_s, 10, 1, s_file) != 1)
            FATAL_ERROR("error: failed to read filesize in \"%s\"\n", s_archiveFilePath.c_str());
        if (std::fread(end_magic, 2, 1, s_file) != 1)
            FATAL_ERROR("error: failed to read end sentinel in \"%s\"\n", s_archiveFilePath.c_str());
        if (std::memcmp(end_magic, expectedEndMagic, 2) != 0)
            FATAL_ERROR("error: corrupted archive header in \"%s\" at \"%s\"\n", s_archiveFilePath.c_str(), file_ident);

        char * ptr = std::strchr(file_ident, '/');
        if (ptr != nullptr)
            *ptr = 0;
        filesize = std::strtoul(filesize_s, nullptr, 10);
        if (std::strncmp(s_archiveObjectPath.c_str(), file_ident, 16) == 0) {
            s_elfFileOffset = std::ftell(s_file);
            return;
        }
        Skip(filesize);
    }

    FATAL_ERROR("error: could not find object \"%s\" in archive \"%s\"\n", s_archiveObjectPath.c_str(), s_archiveFilePath.c_str());
}

static std::string GetSectionName(std::uint32_t shstrtabOffset, int index)
{
    Seek(s_sectionHeaderOffset + s_sectionHeaderEntrySize * index);
    std::uint32_t nameOffset = ReadInt32();
    Seek(shstrtabOffset + nameOffset);
    return ReadString();
}

static void FindTableOffsets()
{
    s_symtabOffset = 0;
    s_strtabOffset = 0;

    Seek(s_sectionHeaderOffset + s_sectionHeaderEntrySize * s_shstrtabIndex + 0x10);
    std::uint32_t shstrtabOffset = ReadInt32();

    for (int i = 0; i < s_sectionCount; i++)
    {
        std::string name = GetSectionName(shstrtabOffset, i);

        if (name == ".symtab")
        {
            if (s_symtabOffset)
                FATAL_ERROR("error: mutiple .symtab sections found in \"%s\"\n", s_elfPath.c_str());
            Seek(s_sectionHeaderOffset + s_sectionHeaderEntrySize * i + 0x10);
            s_symtabOffset = ReadInt32();
            std::uint32_t size = ReadInt32();
            s_symbolCount = size / 16;
        }
        else if (name == ".strtab")
        {
            if (s_strtabOffset)
                FATAL_ERROR("error: mutiple .strtab sections found in \"%s\"\n", s_elfPath.c_str());
            Seek(s_sectionHeaderOffset + s_sectionHeaderEntrySize * i + 0x10);
            s_strtabOffset = ReadInt32();
        }
    }

    if (!s_symtabOffset)
        FATAL_ERROR("error: couldn't find .symtab section in \"%s\"\n", s_elfPath.c_str());

    if (!s_strtabOffset)
        FATAL_ERROR("error: couldn't find .strtab section in \"%s\"\n", s_elfPath.c_str());
}

static std::map<std::string, std::uint32_t> GetCommonSymbols_Shared()
{
    VerifyElfIdent();
    ReadElfHeader();
    FindTableOffsets();

    std::map<std::string, std::uint32_t> commonSymbols;

    std::vector<Symbol> commonSymbolVec;

    Seek(s_symtabOffset);

    for (std::uint32_t i = 0; i < s_symbolCount; i++)
    {
        Symbol sym;
        sym.nameOffset = ReadInt32();
        Skip(4);
        sym.size = ReadInt32();
        Skip(2);
        std::uint16_t sectionIndex = ReadInt16();
        if (sectionIndex == SHN_COMMON)
            commonSymbolVec.push_back(sym);
    }

    for (const Symbol& sym : commonSymbolVec)
    {
        Seek(s_strtabOffset + sym.nameOffset);
        std::string name = ReadString();
        commonSymbols[name] = sym.size;
    }

    return commonSymbols;
}

std::map<std::string, std::uint32_t> GetCommonSymbolsFromLib(std::string sourcePath, std::string libpath)
{
    std::size_t colonPos = libpath.find(':');
    if (colonPos == std::string::npos)
        FATAL_ERROR("error: missing colon separator in libfile \"%s\"\n", s_elfPath.c_str());

    s_archiveObjectPath = libpath.substr(colonPos + 1);
    s_archiveFilePath = sourcePath + "/" + libpath.substr(1, colonPos - 1);
    s_elfPath = sourcePath + "/" + libpath.substr(1);

    s_file = std::fopen(s_archiveFilePath.c_str(), "rb");

    if (s_file == NULL)
        FATAL_ERROR("error: failed to open \"%s\" for reading\n", s_archiveFilePath.c_str());

    VerifyAr();
    FindArObj();
    return GetCommonSymbols_Shared();
}

std::map<std::string, std::uint32_t> GetCommonSymbols(std::string sourcePath, std::string path)
{
    s_elfFileOffset = 0;
    if (path[0] == '*')
        return GetCommonSymbolsFromLib(sourcePath, path);

    s_elfPath = sourcePath + "/" + path;
    s_file = std::fopen(s_elfPath.c_str(), "rb");

    if (s_file == NULL)
        FATAL_ERROR("error: failed to open \"%s\" for reading\n", path.c_str());

    return GetCommonSymbols_Shared();
}