[c21d4d6] | 1 | #
|
---|
| 2 | # Copyright (c) 2005 Martin Decky
|
---|
| 3 | # All rights reserved.
|
---|
| 4 | #
|
---|
| 5 | # Redistribution and use in source and binary forms, with or without
|
---|
| 6 | # modification, are permitted provided that the following conditions
|
---|
| 7 | # are met:
|
---|
| 8 | #
|
---|
| 9 | # - Redistributions of source code must retain the above copyright
|
---|
| 10 | # notice, this list of conditions and the following disclaimer.
|
---|
| 11 | # - Redistributions in binary form must reproduce the above copyright
|
---|
| 12 | # notice, this list of conditions and the following disclaimer in the
|
---|
| 13 | # documentation and/or other materials provided with the distribution.
|
---|
| 14 | # - The name of the author may not be used to endorse or promote products
|
---|
| 15 | # derived from this software without specific prior written permission.
|
---|
| 16 | #
|
---|
| 17 | # THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 18 | # IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 19 | # OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 20 | # IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 21 | # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 22 | # NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 23 | # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 24 | # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 25 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 26 | # THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 27 | #
|
---|
| 28 |
|
---|
[2fff3c4] | 29 |
|
---|
| 30 | arch_src = []
|
---|
| 31 |
|
---|
[63660a3] | 32 | # Fills arch_src.
|
---|
[5f176da] | 33 | subdir('arch' / KARCH)
|
---|
[63660a3] | 34 |
|
---|
| 35 | # Defines genarch_src.
|
---|
[2fff3c4] | 36 | subdir('genarch')
|
---|
[63660a3] | 37 |
|
---|
| 38 | # Defines generic_src, instrumentable_src.
|
---|
[2fff3c4] | 39 | subdir('generic')
|
---|
[63660a3] | 40 |
|
---|
| 41 | # Defines test_src
|
---|
[2fff3c4] | 42 | subdir('test')
|
---|
| 43 |
|
---|
| 44 | ## Cross-platform assembly to start a symtab.data section
|
---|
| 45 | #
|
---|
| 46 | symtab_section = '.section symtab.data, "a", ' + atsign + 'progbits;'
|
---|
| 47 |
|
---|
[63660a3] | 48 | kernel_include_dirs = include_directories(
|
---|
[2fff3c4] | 49 | 'generic/include',
|
---|
| 50 | 'genarch/include',
|
---|
[5f176da] | 51 | 'arch' / KARCH / 'include',
|
---|
| 52 | '..' / 'abi' / 'arch' / KARCH / 'include',
|
---|
[2fff3c4] | 53 | '..' / 'abi' / 'include',
|
---|
| 54 | 'test',
|
---|
| 55 | )
|
---|
| 56 |
|
---|
| 57 | kernel_defs = [
|
---|
[2e2cefdb] | 58 | '-imacros', meson.build_root() / 'config.h',
|
---|
[2fff3c4] | 59 | '-D_HELENOS_SOURCE',
|
---|
| 60 | '-DKERNEL',
|
---|
[6068476] | 61 | '-DHELENOS_RELEASE=' + HELENOS_RELEASE,
|
---|
| 62 | '-DHELENOS_COPYRIGHT=' + HELENOS_COPYRIGHT,
|
---|
| 63 | '-DHELENOS_CODENAME=' + HELENOS_CODENAME,
|
---|
[2fff3c4] | 64 | '-D__@0@_BITS__'.format(meson.get_cross_property('bits')),
|
---|
| 65 | ]
|
---|
| 66 |
|
---|
[63660a3] | 67 | # Preprocess linker script using C preprocessor.
|
---|
[2fff3c4] | 68 | kernel_ldscript = custom_target('_link.ld',
|
---|
[5f176da] | 69 | input: 'arch' / KARCH / '_link.ld.in',
|
---|
[2fff3c4] | 70 | output: '_link.ld',
|
---|
| 71 | command: [
|
---|
| 72 | cc.cmd_array(),
|
---|
[41408d94] | 73 | arch_kernel_c_args,
|
---|
[2fff3c4] | 74 | kernel_defs,
|
---|
[5f176da] | 75 | '-I' + meson.current_source_dir() / 'arch' / KARCH / 'include',
|
---|
[2fff3c4] | 76 | '-D__ASSEMBLER__',
|
---|
| 77 | '-D__LINKER__',
|
---|
| 78 | '-E',
|
---|
| 79 | '-P',
|
---|
| 80 | '-x', 'c',
|
---|
| 81 | '@INPUT@',
|
---|
| 82 | ],
|
---|
| 83 | capture: true,
|
---|
[d3357e9] | 84 | build_by_default: true,
|
---|
[2fff3c4] | 85 | )
|
---|
| 86 |
|
---|
[63660a3] | 87 | kernel_link_args = arch_kernel_link_args + [
|
---|
[2fff3c4] | 88 | '-Wl,--nmagic',
|
---|
| 89 | '-T', meson.current_build_dir() / '_link.ld',
|
---|
| 90 | ]
|
---|
| 91 |
|
---|
[971849b1] | 92 | if CONFIG_LTO
|
---|
| 93 | kernel_link_args += [ '-flto' ]
|
---|
| 94 | endif
|
---|
| 95 |
|
---|
[2fff3c4] | 96 | if CONFIG_STRIP_BINARIES
|
---|
[971849b1] | 97 | # TODO: do this after disassembling
|
---|
[2fff3c4] | 98 | kernel_link_args += [ '-s' ]
|
---|
| 99 | endif
|
---|
| 100 |
|
---|
[63660a3] | 101 | kernel_c_args = arch_kernel_c_args + kernel_defs + [
|
---|
[2fff3c4] | 102 | '-ffreestanding',
|
---|
| 103 | # TODO: remove this flag
|
---|
[2483f28] | 104 | cc.get_supported_arguments([ '-Wno-cast-function-type' ]),
|
---|
[2fff3c4] | 105 | ]
|
---|
| 106 |
|
---|
[971849b1] | 107 | if CONFIG_LTO
|
---|
| 108 | kernel_c_args += [ '-flto' ]
|
---|
| 109 | endif
|
---|
| 110 |
|
---|
[2fff3c4] | 111 | if cc.get_id() == 'clang'
|
---|
| 112 | kernel_c_args += [
|
---|
| 113 | '-fno-stack-protector',
|
---|
| 114 | '-fno-PIC',
|
---|
| 115 | '-mllvm', '-asm-macro-max-nesting-depth=1000',
|
---|
| 116 | ]
|
---|
| 117 | endif
|
---|
| 118 |
|
---|
| 119 | instrumentables = static_library('instrumentables', instrumentable_src,
|
---|
[63660a3] | 120 | include_directories: kernel_include_dirs,
|
---|
[2fff3c4] | 121 | implicit_include_directories: false,
|
---|
| 122 | c_args: kernel_c_args + (CONFIG_TRACE ? [ '-finstrument-functions' ] : []),
|
---|
| 123 | pic: false,
|
---|
| 124 | )
|
---|
| 125 |
|
---|
| 126 | noninstrumentables = static_library('noninstrumentables', arch_src, genarch_src, generic_src, test_src,
|
---|
[63660a3] | 127 | include_directories: kernel_include_dirs,
|
---|
[2fff3c4] | 128 | implicit_include_directories: false,
|
---|
| 129 | c_args: kernel_c_args,
|
---|
| 130 | pic: false,
|
---|
| 131 | )
|
---|
| 132 |
|
---|
| 133 | all_kernel_objects = [ instrumentables, noninstrumentables ]
|
---|
| 134 |
|
---|
| 135 | # We iterate the build several times to get symbol table right.
|
---|
| 136 | # Three times is sufficient to get correct even symbols after symtab.
|
---|
| 137 |
|
---|
| 138 | if CONFIG_SYMTAB
|
---|
| 139 | # Iterate build three times.
|
---|
| 140 | iterations = [ 1, 2, 3 ]
|
---|
| 141 |
|
---|
| 142 | # Generates symbol table information as an object file.
|
---|
| 143 | genmap = find_program('tools/genmap.py')
|
---|
| 144 |
|
---|
| 145 | # Symbol table dump needed for genmap.
|
---|
| 146 | kernel_syms = custom_target('kernel_syms.txt',
|
---|
| 147 | input: all_kernel_objects,
|
---|
| 148 | output: 'kernel_syms.txt',
|
---|
| 149 | command: [ objdump, '-t', '@INPUT@' ],
|
---|
| 150 | capture: true,
|
---|
| 151 | )
|
---|
| 152 | else
|
---|
| 153 | # Build just once.
|
---|
| 154 | iterations = [ 1 ]
|
---|
| 155 | endif
|
---|
| 156 |
|
---|
| 157 | # Empty symbol map for first iteration.
|
---|
| 158 | kernel_map_S = custom_target('empty_map.S',
|
---|
| 159 | output: 'empty_map.S',
|
---|
| 160 | capture: true,
|
---|
[63660a3] | 161 | command: [ 'echo', kernel_as_prolog + symtab_section ],
|
---|
[2fff3c4] | 162 | )
|
---|
| 163 |
|
---|
| 164 | foreach iter : iterations
|
---|
| 165 | is_last = (iter == iterations.length())
|
---|
[b2695b9] | 166 | kernel_name = 'kernel.@0@.elf'.format(iter)
|
---|
[2fff3c4] | 167 | kernel_map_name = kernel_name + '.map'
|
---|
| 168 | kernel_map_path = meson.current_build_dir() / kernel_map_name
|
---|
| 169 |
|
---|
[b2695b9] | 170 | kernel_elf = executable(kernel_name, kernel_map_S,
|
---|
[63660a3] | 171 | include_directories: kernel_include_dirs,
|
---|
[2fff3c4] | 172 | implicit_include_directories: false,
|
---|
| 173 | c_args: kernel_c_args,
|
---|
| 174 | link_args: kernel_c_args + kernel_link_args + [
|
---|
| 175 | '-Wl,-Map,' + kernel_map_path,
|
---|
| 176 | ],
|
---|
| 177 | link_depends: kernel_ldscript,
|
---|
| 178 | link_whole: all_kernel_objects,
|
---|
| 179 | pie: false,
|
---|
| 180 | )
|
---|
| 181 |
|
---|
| 182 | # Generate symbol table if this is not the final iteration.
|
---|
| 183 | if not is_last
|
---|
| 184 |
|
---|
| 185 | # TODO: Teach kernel to read its own ELF symbol table and get rid of this nonsense.
|
---|
| 186 | # Need to first make sure all architectures (even future ones with dumb bootloaders) can use ELF formatted kernel.
|
---|
| 187 |
|
---|
| 188 | kernel_map_bin = custom_target(kernel_map_name + '.bin',
|
---|
| 189 | output: kernel_map_name + '.bin',
|
---|
| 190 | input: [ kernel_elf, kernel_syms ],
|
---|
| 191 | command: [ genmap, kernel_map_path, '@INPUT1@', '@OUTPUT@' ],
|
---|
| 192 | )
|
---|
| 193 |
|
---|
| 194 | kernel_map_S_name = kernel_name + '.map.S'
|
---|
| 195 |
|
---|
| 196 | kernel_map_S = custom_target(kernel_map_S_name,
|
---|
| 197 | input: kernel_map_bin,
|
---|
| 198 | output: kernel_map_S_name,
|
---|
| 199 | capture: true,
|
---|
[63660a3] | 200 | command: [ 'echo', kernel_as_prolog + symtab_section + ' .incbin "@INPUT@"' ],
|
---|
[2fff3c4] | 201 | )
|
---|
| 202 | endif
|
---|
| 203 | endforeach
|
---|
| 204 |
|
---|
[28fcaee] | 205 | rd_init_binaries += [[ kernel_elf, 'boot/kernel.elf' ]]
|
---|
| 206 |
|
---|
[740e952] | 207 | install_files += [[ 'boot', kernel_elf.full_path(), 'kernel.elf' ]]
|
---|
[b2695b9] | 208 | install_deps += [ kernel_elf ]
|
---|
[740e952] | 209 |
|
---|
[b2695b9] | 210 | kernel_disasm = custom_target('kernel.elf.disasm',
|
---|
[2fff3c4] | 211 | command: [ objdump, '-S', '@INPUT@' ],
|
---|
| 212 | input: kernel_elf,
|
---|
[b2695b9] | 213 | output: 'kernel.elf.disasm',
|
---|
[2fff3c4] | 214 | capture: true,
|
---|
| 215 | build_by_default: true,
|
---|
| 216 | )
|
---|
[740e952] | 217 |
|
---|
| 218 | # TODO: Add configuration option for installing debug files
|
---|
| 219 | if false
|
---|
| 220 | install_files += [[ 'boot', kernel_disasm.full_path(), 'kernel.elf.disasm' ]]
|
---|
[b2695b9] | 221 | install_deps += [ kernel_disasm ]
|
---|
[740e952] | 222 | endif
|
---|