image_native.c 4.6 KiB raw
1
/* Native image format of current compilers.
2
 *
3
 * A 48-byte little-endian header, the magic "RADIANCE" followed by an address
4
 * and size for the code and read-only data and an address, size and
5
 * zero-filled tail for read-write data, then the bytes of the three segments.
6
 * Execution starts at the code address. */
7
#include <stdio.h>
8
9
#include "hosted.h"
10
#include "io.h"
11
#include "machine.h"
12
#include "types.h"
13
14
/* Native image magic: the ASCII bytes "RADIANCE" as a little-endian u64. */
15
#define NATIVE_IMAGE_MAGIC 0x45434E4149444152ULL
16
17
/* A native image segment: its address, the bytes stored in the file, and the
18
 * zero-filled bytes that follow them in memory. */
19
struct image_segment {
20
    u64 base;
21
    u32 size;
22
    u32 tail;
23
};
24
25
/* A decoded native image: the code, read-only data and read-write data
26
 * segments, in that order. Execution starts at the code address. */
27
struct image {
28
    struct image_segment segments[3];
29
};
30
31
/* The address range [low, high) that each segment must lie in. */
32
struct image_range {
33
    u64 low[3];
34
    u64 high[3];
35
};
36
37
bool image_native_match(const u8 *header, size_t len) {
38
    return len >= sizeof(u64) && decode_u64_le(header) == NATIVE_IMAGE_MAGIC;
39
}
40
41
/* Decode and check a native image of file_size bytes, leaving the file
42
 * positioned at the payload. The caller has already checked the magic. */
43
static struct image parse_image(
44
    FILE *file, long file_size, const struct image_range *range
45
) {
46
    enum { HEADER_BYTES = 48 };
47
    u8 header[HEADER_BYTES];
48
    rewind(file);
49
    if (fread(header, 1, sizeof(header), file) != sizeof(header))
50
        bail("truncated image header");
51
52
    struct image image;
53
    u64          expected = HEADER_BYTES;
54
    for (u32 i = 0; i < 3; i++) {
55
        const u8             *record  = header + 8 + i * 12;
56
        struct image_segment *seg     = &image.segments[i];
57
        seg->base                     = decode_u64_le(record);
58
        seg->size                     = decode_u32_le(record + 8);
59
        seg->tail                     = i == 2 ? decode_u32_le(record + 12) : 0;
60
        expected                     += seg->size;
61
        u64 extent                    = (u64)seg->size + seg->tail;
62
        if (extent &&
63
            (seg->base < range->low[i] || seg->base >= range->high[i] ||
64
             extent > range->high[i] - seg->base))
65
            bail("image segment outside memory");
66
    }
67
    if (expected != (u64)file_size)
68
        bail("image size does not match header");
69
    return image;
70
}
71
72
/* Read the stored bytes of each segment into memory, with ram_base the address
73
 * that memory offset 0 stands for. The zero-filled tails need no clearing:
74
 * guest memory is static and the image loads once. */
75
static void load_image_segments(
76
    FILE *file, const struct image *image, u64 ram_base
77
) {
78
    for (u32 i = 0; i < 3; i++) {
79
        const struct image_segment *seg = &image->segments[i];
80
        image_read(file, (u32)(seg->base - ram_base), seg->size, "native");
81
    }
82
}
83
84
void image_native_load_machine(
85
    FILE *file, long file_size, u32 memory_size, struct machine_config *config
86
) {
87
    u64                end   = MACHINE_RAM_BASE + memory_size;
88
    struct image_range range = {
89
        .low  = { MACHINE_RAM_BASE, MACHINE_RAM_BASE, MACHINE_RAM_BASE },
90
        .high = { end, end, end },
91
    };
92
    struct image image = parse_image(file, file_size, &range);
93
    load_image_segments(file, &image, MACHINE_RAM_BASE);
94
95
    config->entry                      = image.segments[0].base;
96
    struct machine_section *sections[] = { &config->text,
97
                                           &config->rodata,
98
                                           &config->rwdata };
99
    for (u32 i = 0; i < 3; i++) {
100
        sections[i]->base = image.segments[i].base;
101
        sections[i]->size = image.segments[i].size + image.segments[i].tail;
102
    }
103
}
104
105
struct hosted_image image_native_load(
106
    FILE *file, long file_size, u32 data_limit
107
) {
108
    struct image_range range = {
109
        .low  = { DATA_RO_OFFSET, DATA_RO_OFFSET, DATA_MEMORY_START },
110
        .high = { DATA_MEMORY_START, DATA_MEMORY_START, data_limit },
111
    };
112
    struct image image = parse_image(file, file_size, &range);
113
    load_image_segments(file, &image, 0);
114
115
    const struct image_segment *code   = &image.segments[0];
116
    const struct image_segment *rwdata = &image.segments[2];
117
    u32                         extent = rwdata->size + rwdata->tail;
118
    /* The environment block must lie beyond the end of read-write data. */
119
    return (struct hosted_image){
120
        .code_base  = (u32)code->base,
121
        .code_bytes = code->size,
122
        .data_bytes =
123
            extent ? (u32)(rwdata->base - DATA_MEMORY_START) + extent : 0,
124
    };
125
}