|
| 1 | +/* helpers to combine strings with a parameter */ |
| 2 | +#define COMBINE_STRX(a,b) a##b |
| 3 | +#define COMBINE_STR2(a,b) COMBINE_STRX(a,b) |
| 4 | +#define COMBINE_STR3(a,b,c) COMBINE_STR2(COMBINE_STR2(a,b),c) |
| 5 | + |
| 6 | +/* helper macro to create a section */ |
| 7 | +#define SECTION_IMPL(name, data) \ |
| 8 | + .name : \ |
| 9 | + { \ |
| 10 | + data \ |
| 11 | + } |
| 12 | + |
| 13 | +/* helper macro to create a section with a attribute */ |
| 14 | +#define SECTION_IMPL_ATTR(name, attr, data) \ |
| 15 | + .name (attr) : \ |
| 16 | + { \ |
| 17 | + data \ |
| 18 | + } |
| 19 | + |
| 20 | +/* helper macro to create a alligned section */ |
| 21 | +#define ALLIGNED_SECTION(name, align, data) \ |
| 22 | + SECTION_IMPL( \ |
| 23 | + name, \ |
| 24 | + . = ALIGN(align); \ |
| 25 | + data \ |
| 26 | + ) |
| 27 | + |
| 28 | +/* helper macro to create a alligned readonly section */ |
| 29 | +#define ALLIGNED_READONLY_SECTION(name, align, data) \ |
| 30 | + SECTION_IMPL_ATTR( \ |
| 31 | + name, \ |
| 32 | + READONLY, \ |
| 33 | + . = ALIGN(align); \ |
| 34 | + data \ |
| 35 | + ) |
| 36 | + |
| 37 | +/* macro to define the vectors section. Should be pointed to the correct memory region */ |
| 38 | +#define VECTORS() \ |
| 39 | + ALLIGNED_READONLY_SECTION( \ |
| 40 | + vectors, 4, \ |
| 41 | + KEEP(*(.vectors .vectors.*)); \ |
| 42 | + ) |
| 43 | + |
| 44 | +/* macro to define the rodata section */ |
| 45 | +#define RODATA() \ |
| 46 | + ALLIGNED_READONLY_SECTION( \ |
| 47 | + rodata, 4, \ |
| 48 | + *(.rodata .rodata.* .gnu.linkonce.r.*); \ |
| 49 | + ) |
| 50 | + |
| 51 | +/* macro to define the stack with a size in bytes */ |
| 52 | +#define STACK(size) \ |
| 53 | + .stack (NOLOAD) : \ |
| 54 | + { \ |
| 55 | + . = ALIGN(4); \ |
| 56 | + /* provide the __stack_start */ \ |
| 57 | + PROVIDE(__stack_start = .); \ |
| 58 | + /* add the size in bytes to the current address */ \ |
| 59 | + . = . + size; \ |
| 60 | + /* make sure the size is rounded off to 4 bytes */ \ |
| 61 | + . = ALIGN(4); \ |
| 62 | + /* provide the __stack_end */ \ |
| 63 | + PROVIDE(__stack_end = .); \ |
| 64 | + } |
| 65 | + |
| 66 | +/* macro to define a init array. Array name is passed as an argument */ |
| 67 | +#define CONSTRUCTOR_SECTION(name) \ |
| 68 | + .name : \ |
| 69 | + { \ |
| 70 | + . = ALIGN(4); \ |
| 71 | + PROVIDE(COMBINE_STR3(__,name,_start) = .); \ |
| 72 | + KEEP(*(SORT(.name.*))) \ |
| 73 | + KEEP(*(SORT(.name))) \ |
| 74 | + PROVIDE(COMBINE_STR3(__,name,_end) = .); \ |
| 75 | + } |
| 76 | + |
| 77 | +/* macro to define a data section. Note: this section is not automaticly loaded. |
| 78 | + The secondary loaded needs to be enabled to load this at startup after the |
| 79 | + startup code */ |
| 80 | +#define DATA_SECTION(name, region, storage) \ |
| 81 | + COMBINE_STR2(.name,_data) : \ |
| 82 | + { \ |
| 83 | + . = ALIGN(4); \ |
| 84 | + PROVIDE(COMBINE_STR3(__,name,_data_rom_start) = LOADADDR(COMBINE_STR2(.name,_data))); \ |
| 85 | + PROVIDE(COMBINE_STR3(__,name,_data_start) = .); \ |
| 86 | + *(SORT_BY_ALIGNMENT(COMBINE_STR2(.name,_data))); \ |
| 87 | + *(SORT_BY_ALIGNMENT(COMBINE_STR2(.name,_data.*))); \ |
| 88 | + . = ALIGN(4); \ |
| 89 | + PROVIDE(COMBINE_STR3(__,name,_data_end) = .); \ |
| 90 | + } > region AT > storage |
| 91 | + |
| 92 | +/* data section entry for the multi section table */ |
| 93 | +#define DATA_SECTION_ENTRY(name) \ |
| 94 | + LONG(COMBINE_STR3(__,name,_data_rom_start)); \ |
| 95 | + LONG(COMBINE_STR3(__,name,_data_start)); \ |
| 96 | + LONG(COMBINE_STR3(__,name,_data_end)); |
| 97 | + |
| 98 | +/* end marker for the multi section table */ |
| 99 | +#define DATA_SECTION_ENTRY_END() \ |
| 100 | + LONG(0); \ |
| 101 | + LONG(0); \ |
| 102 | + LONG(0); |
| 103 | + |
| 104 | +/* multisection table to automaticly load the data into the section location */ |
| 105 | +#define DATA_MULTISECTION_TABLE(entries) \ |
| 106 | + ALLIGNED_READONLY_SECTION( \ |
| 107 | + multisection_data, 4, \ |
| 108 | + PROVIDE(__multisection_data_start = .); \ |
| 109 | + entries \ |
| 110 | + PROVIDE(__multisection_data_end = .); \ |
| 111 | + ) |
| 112 | + |
| 113 | +#define BSS_SECTION(name, region) \ |
| 114 | + COMBINE_STR2(.name,_bss) (NOLOAD) : \ |
| 115 | + { \ |
| 116 | + . = ALIGN(4); \ |
| 117 | + PROVIDE(COMBINE_STR3(__,name,_bss_start) = .); \ |
| 118 | + *(SORT_BY_ALIGNMENT(COMBINE_STR2(.name,_bss))); \ |
| 119 | + *(SORT_BY_ALIGNMENT(COMBINE_STR2(.name,_bss.*))); \ |
| 120 | + . = ALIGN(4); \ |
| 121 | + PROVIDE(COMBINE_STR3(__,name,_bss_end) = .); \ |
| 122 | + } > region |
| 123 | + |
| 124 | +/* bss section entry for the multi section table */ |
| 125 | +#define BSS_SECTION_ENTRY(name) \ |
| 126 | + LONG(COMBINE_STR3(__,name,_bss_start)); \ |
| 127 | + LONG(COMBINE_STR3(__,name,_bss_end)); |
| 128 | + |
| 129 | +/* end marker for the multi section table */ |
| 130 | +#define BSS_SECTION_ENTRY_END() \ |
| 131 | + LONG(0); \ |
| 132 | + LONG(0); |
| 133 | + |
| 134 | +/* multisection table to automaticly load all the bss sections to zero */ |
| 135 | +#define BSS_MULTISECTION_TABLE(entries) \ |
| 136 | + ALLIGNED_READONLY_SECTION( \ |
| 137 | + multisection_bss, 4, \ |
| 138 | + PROVIDE(__multisection_bss_start = .); \ |
| 139 | + entries \ |
| 140 | + PROVIDE(__multisection_bss_end = .); \ |
| 141 | + ) |
0 commit comments