summaryrefslogtreecommitdiff
path: root/src/readcoff.cpp
blob: f9398c41530aab34bf758cc8d0fa3c87ed10476f (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
/* SPDX-License-Identifier: Unlicense
 */

#include "coff_image.h"
#include "data_buffer.h"

#define OPTPARSE_IMPLEMENTATION
#define OPTPARSE_API static
#ifdef __GNUC__
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wold-style-cast"
#pragma GCC diagnostic ignored "-Wshadow"
#endif
#include "optparse/optparse.h"
#ifdef __GNUC__
#pragma GCC diagnostic pop
#endif

#include <cassert>
#include <cerrno>
#include <cstdio>
#include <cstdlib>
#include <cstring>

static bool g_print_header = false;
static bool g_print_optional_header = false;
static bool g_print_section_headers = false;

static void PrintCoffHeader(FILE *output_stream, const COFF::FileHeader &header)
{
    fprintf(output_stream, "COFF Header:\n");
    fprintf(output_stream, "  Magic:   %02x %02x (0x%x as big endian)\n",
            (header.magic >> 8) & 0xff, header.magic & 0xff, header.magic);
    fprintf(output_stream, "  Number of section headers:   %u\n", header.nsections);
    // TODO proper time with year, month, day, hour, minute and second
    fprintf(output_stream, "  Time and date:               %u\n", header.timedate);
    fprintf(output_stream, "  Symbol table file offset:    %u (0x%x)\n",
            header.symtable_offset, header.symtable_offset);
    fprintf(output_stream, "  Number of symbols:           %u (0x%x)\n",
            header.nsymbols, header.nsymbols);
    fprintf(output_stream, "  Optional header size, bytes: %u (0x%x)\n",
            header.optional_header_nbytes, header.optional_header_nbytes);
    // TODO Print detailed flags information
    fprintf(output_stream, "  Flags:                       0x%x\n", header.flags);
}

static void PrintOptionalHeader(FILE *output_stream, const COFF::OptionalHeader &header)
{
    fprintf(output_stream, "Optional Header:\n");
    fprintf(output_stream, "  Magic:   %02x %02x (0x%x as big endian)\n",
            (header.magic >> 8) & 0xff, header.magic & 0xff, header.magic);
    fprintf(output_stream, "  Version:                           0x%04x\n", header.version);
    fprintf(output_stream, "  Size of text, bytes:               %u\n", header.tsize);
    fprintf(output_stream, "  Size of initialized data, bytes:   %u\n", header.dsize);
    fprintf(output_stream, "  Size of uninitialized data, bytes: %u\n", header.bsize);
    fprintf(output_stream, "  Program entry point:               %u (0x%x)\n",
            header.entry, header.entry);
    fprintf(output_stream, "  Base address of text:              %u (0x%x)\n",
            header.text_start, header.text_start);
    fprintf(output_stream, "  Base address of daata:             %u (0x%x)\n",
            header.data_start, header.data_start);
}

static const char *SectionTypeStr(COFF::SectionType type)
{
    switch (type) {
    case COFF::SectionType::Regular:
        return "Regular  ARL";
    case COFF::SectionType::Dummy:
        return "Dubby     R";
    case COFF::SectionType::NoLoad:
        return "NoLoad   AR";
    case COFF::SectionType::Grouped:
        return "Grouped";
    case COFF::SectionType::Padding:
        return "Padding    L";
    case COFF::SectionType::Copy:
        return "Cppy     A L";
    case COFF::SectionType::Text:
        return "Text     ARL";
    case COFF::SectionType::Data:
        return "Data     ARL";
    case COFF::SectionType::Bss:
        return "BSS      ARL";
    case COFF::SectionType::Org:
        return "Org      ARL";
    case COFF::SectionType::Info:
        return "Info";
    case COFF::SectionType::Overlay:
        return "Ovsrlay   R";
    case COFF::SectionType::Lib:
        return "Lib      ARL";
    }
    return "Unknwon";
}

static void PrintSectionHeader(FILE *output_stream, size_t index, const COFF::SectionHeader &header)
{
    fprintf(output_stream,
            "  [%2zu] %-8s ""%08x "  "%08x "  "%08x "  "%08x "   "%08x " "\n",
            index,
            header.name,
            header.paddr,
            header.vaddr,
            header.size,
            header.section_offset,
            header.reloc_offset);
    fprintf(output_stream,
            "       %08x "  "    %04x     %04x %08x "  "%s\n",
            header.lineno_offset,
            header.nreloc,
            header.nlineno,
            header.flags,
            SectionTypeStr(static_cast<COFF::SectionType>(header.flags & 0x8ff)));
}

static int ReadCoff(FILE *input_stream, FILE *output_stream)
{
    auto input = DataBuffer::FromStream(input_stream);
    const size_t input_size = input.occupied_size;
    if (input_size == 0) {
        fprintf(stderr, "DataBuffer::FromStream(input, input_stream): "
                "Error: No data has been read\n");
        return EXIT_FAILURE;
    }
    const COFF::Image coff(static_cast<DataBuffer&&>(input));
    if (!coff.IsValid()) {
        fprintf(stderr, "Error: COFF image is not valid: %s\n", coff.Error());
        return EXIT_FAILURE;
    }
    const auto &file_header = coff.FileHeader();
    if (g_print_header) {
        PrintCoffHeader(output_stream, file_header);
    }
    if (g_print_optional_header && coff.HasOptionalHeader()) {
        PrintOptionalHeader(output_stream, coff.OptionalHeader());
    }
    if (g_print_section_headers && file_header.nsections) {
        fprintf(output_stream, "Section headers:\n");
        fprintf(output_stream,
                "  [Nr] Name     PhysAddr VirtAddr Size     DatOffst RelOffst\n"
                "       LnNoOfft NReloc   NLineNo  Flags    Type     Alloc/Reloc/Load\n");
        for (size_t i = 0; i < file_header.nsections; i++) {
            PrintSectionHeader(output_stream, i, coff.GetSectionHeader(i));
        }
    }
    return EXIT_SUCCESS;
}

static void PrintUsage(FILE *s, const char *argv0)
{
    // Please, keep all lines in 80 columns range when printed.
    fprintf(s,
    "Usage: %s [options] <input-file-name>\n"
    "Options:\n"
    "  -H, --help            Show this message.\n"
    "  -a --all              Equivalent to: -h -o -S\n"
    "  -h --file-header      Display the COFF file header\n"
    "  -o --optional-header  Display the optional COFF file header\n"
    "  -S --section-headers  Display the sections' header\n"
    "     --sections         An alias for --section-headers\n"
    "  -e --headers          Equivalent to: -h -o -S\n"
    "  <input_file_name>     COFF file with the machine code to extract\n"
    "                        ('-' means stdin).\n"
    , argv0);
}

int main(int, char* argv[])
{
    struct optparse_long longopts[] = {
        {"help", 'H', OPTPARSE_NONE},
        {"all", 'a', OPTPARSE_NONE},
        {"file-header", 'h', OPTPARSE_NONE},
        {"optional-header", 'o', OPTPARSE_NONE},
        {"section-headers", 'S', OPTPARSE_NONE},
        {"sections", 0x80, OPTPARSE_NONE},
        {"headers", 'e', OPTPARSE_NONE},
        {},
    };
    const char *input_file_name = nullptr;
    struct optparse options;
    optparse_init(&options, argv);
    // Parse opts
    int option;
    while ((option = optparse_long(&options, longopts, NULL)) != -1) {
        switch (option) {
        case 'H':
            PrintUsage(stdout, argv[0]);
            return EXIT_SUCCESS;
            break;
        case 'h':
            g_print_header = true;
            break;
        case 'o':
            g_print_optional_header = true;
            break;
        case 0x80:
        case 'S':
            g_print_section_headers = true;
            break;
        case 'a':
        case 'e':
            g_print_header = true;
            g_print_optional_header = true;
            g_print_section_headers = true;
            break;
        case '?':
            fprintf(stderr, "main: optparse_long: Error: \"%s\"\n", options.errmsg);
            return EXIT_FAILURE;
        }
    }
    // Parse input file name
    char *arg;
    while ((arg = optparse_arg(&options))) {
        if (input_file_name == nullptr) {
            input_file_name = arg;
        } else {
            fprintf(stderr, "error: too many free arguments provided\n");
            return EXIT_FAILURE;
        }
    }
    // Open the files
    FILE *input_stream = nullptr;
    FILE *output_stream = stdout;
    if (input_file_name) {
        if (0 == strcmp(input_file_name, "-")) {
            input_stream = stdin;
        } else {
            input_stream = fopen(input_file_name, "r");
        }
        if (input_stream == nullptr) {
            const int err = errno;
            fprintf(stderr, "main: fopen(\"%s\", \"r\"): Error (%d): \"%s\"\n", input_file_name, err, strerror(err));
            return EXIT_FAILURE;
        }
    } else {
        fprintf(stderr, "main: Error: no input file name specified, see usage below.\n");
        PrintUsage(stderr, argv[0]);
        return EXIT_FAILURE;
    }
    if (!g_print_header && !g_print_optional_header && !g_print_section_headers) {
        fprintf(stderr, "main: Error: no display options specified, see usage below.\n");
        PrintUsage(stdout, argv[0]);
        return EXIT_FAILURE;
    }
    // Run the program
    const int ret = ReadCoff(input_stream, output_stream);
    fclose(output_stream);
    fclose(input_stream);
    return ret;
}