|
| 1 | +/* An example linker script for Rust Keystone enclave application |
| 2 | + * |
| 3 | + * SPDX-License-Identifier: MIT |
| 4 | + * Copyright (C) 2022 VTT Technical Research Centre of Finland Ltd |
| 5 | + */ |
| 6 | +OUTPUT_ARCH( "riscv" ) |
| 7 | + |
| 8 | +/* Use linker argument '--defsym HEAP_SIZE=<size>' to alter heap size. The size |
| 9 | + * must a multiple of page size (4096 bytes). Default value is 0. */ |
| 10 | +EXTERN(HEAP_SIZE) |
| 11 | + |
| 12 | +ASSERT(DEFINED(HEAP_SIZE) ? HEAP_SIZE % 4096 == 0 : 1, |
| 13 | + "HEAP_SIZE must be multiple of page size (4096 bytes)") |
| 14 | + |
| 15 | +/* Use linker argument '--defsym ECALL_INPUT_SIZE=<size>' to alter the amount of |
| 16 | + * space reserved for ecall input buffer. This option is only needed if feature |
| 17 | + * "heap_rt" is NOT used. Default value is 0. |
| 18 | + */ |
| 19 | +EXTERN(ECALL_INPUT_SIZE) |
| 20 | + |
| 21 | +/* Use linker argument '--defsym ECALL_OUTPUT_SIZE=<size>' to alter the amount |
| 22 | + * of space reserved for ecall output buffer. This option is only needed if |
| 23 | + * feature "heap_rt" is NOT used. Default value is 0. |
| 24 | + */ |
| 25 | +EXTERN(ECALL_OUTPUT_SIZE) |
| 26 | + |
| 27 | +/* ECall header is always 20 bytes */ |
| 28 | +ECALL_HEADER_LENGTH = 24; |
| 29 | +/* Length of the ecall input buffer in bytes */ |
| 30 | +ECALL_IBUF_SIZE = DEFINED(ECALL_INPUT_SIZE) |
| 31 | + ? ECALL_INPUT_SIZE + ECALL_HEADER_LENGTH |
| 32 | + : 0; |
| 33 | +/* Length of the ecall output buffer in bytes */ |
| 34 | +ECALL_OBUF_SIZE = DEFINED(ECALL_OUTPUT_SIZE) |
| 35 | + ? ECALL_OUTPUT_SIZE + ECALL_HEADER_LENGTH |
| 36 | + : 0; |
| 37 | + |
| 38 | +/* Program's actual entry point before eapp_entry. Defined in libeapp */ |
| 39 | +ENTRY(_start) |
| 40 | + |
| 41 | +PHDRS |
| 42 | +{ |
| 43 | +/* phdrs PT_PHDR PHDRS; */ |
| 44 | + text PT_LOAD FILEHDR PHDRS FLAGS(5); |
| 45 | + data PT_LOAD FLAGS (6); |
| 46 | + alloc PT_LOAD FLAGS (6); |
| 47 | + bss PT_LOAD; |
| 48 | +} |
| 49 | + |
| 50 | +SECTIONS |
| 51 | +{ |
| 52 | + /* The text section must be aligned to page boundary. Keystone |
| 53 | + * runtime will check it. |
| 54 | + */ |
| 55 | + . = 0x00001000; |
| 56 | + .text : { |
| 57 | + /* The _start function should always be at address 0x00001000 */ |
| 58 | + *(.text._start) |
| 59 | + *(.text) |
| 60 | + *(.text.*) |
| 61 | + } : text |
| 62 | + .rodata : |
| 63 | + { |
| 64 | + *(.rdata) |
| 65 | + *(.rodata) |
| 66 | + *(.rodata.*) |
| 67 | + } /* defaults to .text */ |
| 68 | + . = ALIGN(0x1000); |
| 69 | + .data : { *(.data) } : data |
| 70 | + .debug : { *(.debug) } |
| 71 | + |
| 72 | + /* Ecall buffers, in case feature "heap_rt" is not used */ |
| 73 | + . = ALIGN(0x1000); |
| 74 | + .ecall_zone (NOLOAD): |
| 75 | + { |
| 76 | + *(.ecall_zone); |
| 77 | + PROVIDE(__ecall_inbuf_end = DEFINED(__ecall_inbuf_start) |
| 78 | + ? . + ECALL_IBUF_SIZE |
| 79 | + : . ); |
| 80 | + PROVIDE(__ecall_outbuf_start = DEFINED(__ecall_inbuf_start) |
| 81 | + ? . + ECALL_IBUF_SIZE |
| 82 | + : . ); |
| 83 | + PROVIDE(__ecall_outbuf_end = DEFINED(__ecall_inbuf_start) |
| 84 | + ? . + ECALL_IBUF_SIZE + ECALL_OBUF_SIZE |
| 85 | + : . ); |
| 86 | + /* This statement enforces correct section size since the location counter |
| 87 | + * is not moved otherwise (only symbols are defined). However, if none of |
| 88 | + * the symbols are needed and .ecall_zone is effectively empty, this |
| 89 | + * statement also causes the linker not to remove the section (TODO?) |
| 90 | + */ |
| 91 | + . = DEFINED(__ecall_inbuf_start) |
| 92 | + ? . + ECALL_IBUF_SIZE + ECALL_OBUF_SIZE |
| 93 | + : . ; |
| 94 | + } : alloc |
| 95 | + |
| 96 | + /* Section .malloc_zone and its symbols are used by the tiny malloc |
| 97 | + * included in the Keystone enclave application libraries. |
| 98 | + */ |
| 99 | + . = ALIGN(0x1000); |
| 100 | + .malloc_zone (NOLOAD): |
| 101 | + { |
| 102 | + __malloc_zone_start = .; |
| 103 | + /* '__malloc_start' defined in libeapp will be placed here: */ |
| 104 | + *(.malloc_zone); |
| 105 | + PROVIDE(__malloc_start = .); |
| 106 | + . = DEFINED(HEAP_SIZE) ? (. + HEAP_SIZE) : . ; |
| 107 | + PROVIDE(__malloc_zone_stop = .); |
| 108 | + } : alloc |
| 109 | + |
| 110 | + . = ALIGN(0x4); |
| 111 | + .bss : { *(.bss) |
| 112 | + *(.bss.*) } : bss |
| 113 | + |
| 114 | + /* Unless specified, the linker by default outputs the .eh_frame section |
| 115 | + * before the text section, which moves the text section from the page |
| 116 | + * boundary, causing trouble with Eyrie, as the _start function will shift |
| 117 | + * from its intended address 0x00001000. |
| 118 | + */ |
| 119 | + /DISCARD/ : { |
| 120 | + *(.eh_frame) |
| 121 | + } |
| 122 | + _end = .; |
| 123 | +} |
0 commit comments