/* Native image format of current compilers. * * A 48-byte little-endian header, the magic "RADIANCE" followed by an address * and size for the code and read-only data and an address, size and * zero-filled tail for read-write data, then the bytes of the three segments. * Execution starts at the code address. */ #include #include "hosted.h" #include "io.h" #include "machine.h" #include "types.h" /* Native image magic: the ASCII bytes "RADIANCE" as a little-endian u64. */ #define NATIVE_IMAGE_MAGIC 0x45434E4149444152ULL /* A native image segment: its address, the bytes stored in the file, and the * zero-filled bytes that follow them in memory. */ struct image_segment { u64 base; u32 size; u32 tail; }; /* A decoded native image: the code, read-only data and read-write data * segments, in that order. Execution starts at the code address. */ struct image { struct image_segment segments[3]; }; /* The address range [low, high) that each segment must lie in. */ struct image_range { u64 low[3]; u64 high[3]; }; bool image_native_match(const u8 *header, size_t len) { return len >= sizeof(u64) && decode_u64_le(header) == NATIVE_IMAGE_MAGIC; } /* Decode and check a native image of file_size bytes, leaving the file * positioned at the payload. The caller has already checked the magic. */ static struct image parse_image( FILE *file, long file_size, const struct image_range *range ) { enum { HEADER_BYTES = 48 }; u8 header[HEADER_BYTES]; rewind(file); if (fread(header, 1, sizeof(header), file) != sizeof(header)) bail("truncated image header"); struct image image; u64 expected = HEADER_BYTES; for (u32 i = 0; i < 3; i++) { const u8 *record = header + 8 + i * 12; struct image_segment *seg = &image.segments[i]; seg->base = decode_u64_le(record); seg->size = decode_u32_le(record + 8); seg->tail = i == 2 ? decode_u32_le(record + 12) : 0; expected += seg->size; u64 extent = (u64)seg->size + seg->tail; if (extent && (seg->base < range->low[i] || seg->base >= range->high[i] || extent > range->high[i] - seg->base)) bail("image segment outside memory"); } if (expected != (u64)file_size) bail("image size does not match header"); return image; } /* Read the stored bytes of each segment into memory, with ram_base the address * that memory offset 0 stands for. The zero-filled tails need no clearing: * guest memory is static and the image loads once. */ static void load_image_segments( FILE *file, const struct image *image, u64 ram_base ) { for (u32 i = 0; i < 3; i++) { const struct image_segment *seg = &image->segments[i]; image_read(file, (u32)(seg->base - ram_base), seg->size, "native"); } } void image_native_load_machine( FILE *file, long file_size, u32 memory_size, struct machine_config *config ) { u64 end = MACHINE_RAM_BASE + memory_size; struct image_range range = { .low = { MACHINE_RAM_BASE, MACHINE_RAM_BASE, MACHINE_RAM_BASE }, .high = { end, end, end }, }; struct image image = parse_image(file, file_size, &range); load_image_segments(file, &image, MACHINE_RAM_BASE); config->entry = image.segments[0].base; struct machine_section *sections[] = { &config->text, &config->rodata, &config->rwdata }; for (u32 i = 0; i < 3; i++) { sections[i]->base = image.segments[i].base; sections[i]->size = image.segments[i].size + image.segments[i].tail; } } struct hosted_image image_native_load( FILE *file, long file_size, u32 data_limit ) { struct image_range range = { .low = { DATA_RO_OFFSET, DATA_RO_OFFSET, DATA_MEMORY_START }, .high = { DATA_MEMORY_START, DATA_MEMORY_START, data_limit }, }; struct image image = parse_image(file, file_size, &range); load_image_segments(file, &image, 0); const struct image_segment *code = &image.segments[0]; const struct image_segment *rwdata = &image.segments[2]; u32 extent = rwdata->size + rwdata->tail; /* The environment block must lie beyond the end of read-write data. */ return (struct hosted_image){ .code_base = (u32)code->base, .code_bytes = code->size, .data_bytes = extent ? (u32)(rwdata->base - DATA_MEMORY_START) + extent : 0, }; }