| 1 | #
|
|---|
| 2 | # SPDX-FileCopyrightText: 2005 Martin Decky
|
|---|
| 3 | #
|
|---|
| 4 | # SPDX-License-Identifier: BSD-3-Clause
|
|---|
| 5 | #
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 | arch_src = []
|
|---|
| 9 |
|
|---|
| 10 | # Fills arch_src.
|
|---|
| 11 | subdir('arch' / KARCH)
|
|---|
| 12 |
|
|---|
| 13 | # Defines genarch_src.
|
|---|
| 14 | subdir('genarch')
|
|---|
| 15 |
|
|---|
| 16 | # Defines generic_src, instrumentable_src.
|
|---|
| 17 | subdir('generic')
|
|---|
| 18 |
|
|---|
| 19 | # Defines test_src
|
|---|
| 20 | subdir('test')
|
|---|
| 21 |
|
|---|
| 22 | ## Cross-platform assembly to start a symtab.data section
|
|---|
| 23 | #
|
|---|
| 24 | symtab_section = '.section symtab.data, "a", ' + atsign + 'progbits;'
|
|---|
| 25 |
|
|---|
| 26 | kernel_include_dirs = include_directories(
|
|---|
| 27 | 'generic/include',
|
|---|
| 28 | 'genarch/include',
|
|---|
| 29 | 'arch' / KARCH / 'include',
|
|---|
| 30 | '..' / 'abi' / 'arch' / KARCH / 'include',
|
|---|
| 31 | '..' / 'abi' / 'include',
|
|---|
| 32 | 'test',
|
|---|
| 33 | )
|
|---|
| 34 |
|
|---|
| 35 | kernel_defs = [
|
|---|
| 36 | '-imacros', meson.build_root() / 'config.h',
|
|---|
| 37 | '-D_HELENOS_SOURCE',
|
|---|
| 38 | '-DKERNEL',
|
|---|
| 39 | '-DHELENOS_RELEASE=' + HELENOS_RELEASE,
|
|---|
| 40 | '-DHELENOS_COPYRIGHT=' + HELENOS_COPYRIGHT,
|
|---|
| 41 | '-DHELENOS_CODENAME=' + HELENOS_CODENAME,
|
|---|
| 42 | '-D__@0@_BITS__'.format(meson.get_cross_property('bits')),
|
|---|
| 43 | ]
|
|---|
| 44 |
|
|---|
| 45 | # Preprocess linker script using C preprocessor.
|
|---|
| 46 | kernel_ldscript = custom_target('_link.ld',
|
|---|
| 47 | input: 'arch' / KARCH / '_link.ld.in',
|
|---|
| 48 | output: '_link.ld',
|
|---|
| 49 | command: [
|
|---|
| 50 | cc.cmd_array(),
|
|---|
| 51 | arch_kernel_c_args,
|
|---|
| 52 | kernel_defs,
|
|---|
| 53 | '-I' + meson.current_source_dir() / 'arch' / KARCH / 'include',
|
|---|
| 54 | '-D__ASSEMBLER__',
|
|---|
| 55 | '-D__LINKER__',
|
|---|
| 56 | '-E',
|
|---|
| 57 | '-P',
|
|---|
| 58 | '-x', 'c',
|
|---|
| 59 | '@INPUT@',
|
|---|
| 60 | ],
|
|---|
| 61 | capture: true,
|
|---|
| 62 | build_by_default: true,
|
|---|
| 63 | )
|
|---|
| 64 |
|
|---|
| 65 | kernel_link_args = arch_kernel_link_args + [
|
|---|
| 66 | '-Wl,--nmagic',
|
|---|
| 67 | '-T', meson.current_build_dir() / '_link.ld',
|
|---|
| 68 | ]
|
|---|
| 69 |
|
|---|
| 70 | if CONFIG_LTO
|
|---|
| 71 | kernel_link_args += [ '-flto' ]
|
|---|
| 72 | endif
|
|---|
| 73 |
|
|---|
| 74 | if CONFIG_STRIP_BINARIES
|
|---|
| 75 | # TODO: do this after disassembling
|
|---|
| 76 | kernel_link_args += [ '-s' ]
|
|---|
| 77 | endif
|
|---|
| 78 |
|
|---|
| 79 | kernel_c_args = arch_kernel_c_args + kernel_defs + [
|
|---|
| 80 | '-ffreestanding',
|
|---|
| 81 | # TODO: remove this flag
|
|---|
| 82 | cc.get_supported_arguments([ '-Wno-cast-function-type' ]),
|
|---|
| 83 | ]
|
|---|
| 84 |
|
|---|
| 85 | if CONFIG_LTO
|
|---|
| 86 | kernel_c_args += [ '-flto' ]
|
|---|
| 87 | endif
|
|---|
| 88 |
|
|---|
| 89 | if cc.get_id() == 'clang'
|
|---|
| 90 | kernel_c_args += [
|
|---|
| 91 | '-fno-stack-protector',
|
|---|
| 92 | '-fno-PIC',
|
|---|
| 93 | '-mllvm', '-asm-macro-max-nesting-depth=1000',
|
|---|
| 94 | ]
|
|---|
| 95 | endif
|
|---|
| 96 |
|
|---|
| 97 | instrumentables = static_library('instrumentables', instrumentable_src,
|
|---|
| 98 | include_directories: kernel_include_dirs,
|
|---|
| 99 | implicit_include_directories: false,
|
|---|
| 100 | c_args: kernel_c_args + (CONFIG_TRACE ? [ '-finstrument-functions' ] : []),
|
|---|
| 101 | pic: false,
|
|---|
| 102 | )
|
|---|
| 103 |
|
|---|
| 104 | noninstrumentables = static_library('noninstrumentables', arch_src, genarch_src, generic_src, test_src,
|
|---|
| 105 | include_directories: kernel_include_dirs,
|
|---|
| 106 | implicit_include_directories: false,
|
|---|
| 107 | c_args: kernel_c_args,
|
|---|
| 108 | pic: false,
|
|---|
| 109 | )
|
|---|
| 110 |
|
|---|
| 111 | all_kernel_objects = [ instrumentables, noninstrumentables ]
|
|---|
| 112 |
|
|---|
| 113 | # We iterate the build several times to get symbol table right.
|
|---|
| 114 | # Three times is sufficient to get correct even symbols after symtab.
|
|---|
| 115 |
|
|---|
| 116 | if CONFIG_SYMTAB
|
|---|
| 117 | # Iterate build three times.
|
|---|
| 118 | iterations = [ 1, 2, 3 ]
|
|---|
| 119 |
|
|---|
| 120 | # Generates symbol table information as an object file.
|
|---|
| 121 | genmap = find_program('tools/genmap.py')
|
|---|
| 122 |
|
|---|
| 123 | # Symbol table dump needed for genmap.
|
|---|
| 124 | kernel_syms = custom_target('kernel_syms.txt',
|
|---|
| 125 | input: all_kernel_objects,
|
|---|
| 126 | output: 'kernel_syms.txt',
|
|---|
| 127 | command: [ objdump, '-t', '@INPUT@' ],
|
|---|
| 128 | capture: true,
|
|---|
| 129 | )
|
|---|
| 130 | else
|
|---|
| 131 | # Build just once.
|
|---|
| 132 | iterations = [ 1 ]
|
|---|
| 133 | endif
|
|---|
| 134 |
|
|---|
| 135 | # Empty symbol map for first iteration.
|
|---|
| 136 | kernel_map_S = custom_target('empty_map.S',
|
|---|
| 137 | output: 'empty_map.S',
|
|---|
| 138 | capture: true,
|
|---|
| 139 | command: [ 'echo', kernel_as_prolog + symtab_section ],
|
|---|
| 140 | )
|
|---|
| 141 |
|
|---|
| 142 | foreach iter : iterations
|
|---|
| 143 | is_last = (iter == iterations.length())
|
|---|
| 144 | kernel_name = 'kernel.@0@.elf'.format(iter)
|
|---|
| 145 | kernel_map_name = kernel_name + '.map'
|
|---|
| 146 | kernel_map_path = meson.current_build_dir() / kernel_map_name
|
|---|
| 147 |
|
|---|
| 148 | kernel_elf = executable(kernel_name, kernel_map_S,
|
|---|
| 149 | include_directories: kernel_include_dirs,
|
|---|
| 150 | implicit_include_directories: false,
|
|---|
| 151 | c_args: kernel_c_args,
|
|---|
| 152 | link_args: kernel_c_args + kernel_link_args + [
|
|---|
| 153 | '-Wl,-Map,' + kernel_map_path,
|
|---|
| 154 | ],
|
|---|
| 155 | link_depends: kernel_ldscript,
|
|---|
| 156 | link_whole: all_kernel_objects,
|
|---|
| 157 | pie: false,
|
|---|
| 158 | )
|
|---|
| 159 |
|
|---|
| 160 | # Generate symbol table if this is not the final iteration.
|
|---|
| 161 | if not is_last
|
|---|
| 162 |
|
|---|
| 163 | # TODO: Teach kernel to read its own ELF symbol table and get rid of this nonsense.
|
|---|
| 164 | # Need to first make sure all architectures (even future ones with dumb bootloaders) can use ELF formatted kernel.
|
|---|
| 165 |
|
|---|
| 166 | kernel_map_bin = custom_target(kernel_map_name + '.bin',
|
|---|
| 167 | output: kernel_map_name + '.bin',
|
|---|
| 168 | input: [ kernel_elf, kernel_syms ],
|
|---|
| 169 | command: [ genmap, kernel_map_path, '@INPUT1@', '@OUTPUT@' ],
|
|---|
| 170 | )
|
|---|
| 171 |
|
|---|
| 172 | kernel_map_S_name = kernel_name + '.map.S'
|
|---|
| 173 |
|
|---|
| 174 | kernel_map_S = custom_target(kernel_map_S_name,
|
|---|
| 175 | input: kernel_map_bin,
|
|---|
| 176 | output: kernel_map_S_name,
|
|---|
| 177 | capture: true,
|
|---|
| 178 | command: [ 'echo', kernel_as_prolog + symtab_section + ' .incbin "@INPUT@"' ],
|
|---|
| 179 | )
|
|---|
| 180 | endif
|
|---|
| 181 | endforeach
|
|---|
| 182 |
|
|---|
| 183 | rd_init_binaries += [[ kernel_elf, 'boot/kernel.elf' ]]
|
|---|
| 184 |
|
|---|
| 185 | install_files += [[ 'boot', kernel_elf.full_path(), 'kernel.elf' ]]
|
|---|
| 186 | install_deps += [ kernel_elf ]
|
|---|
| 187 |
|
|---|
| 188 | kernel_disasm = custom_target('kernel.elf.disasm',
|
|---|
| 189 | command: [ objdump, '-S', '@INPUT@' ],
|
|---|
| 190 | input: kernel_elf,
|
|---|
| 191 | output: 'kernel.elf.disasm',
|
|---|
| 192 | capture: true,
|
|---|
| 193 | build_by_default: true,
|
|---|
| 194 | )
|
|---|
| 195 |
|
|---|
| 196 | # TODO: Add configuration option for installing debug files
|
|---|
| 197 | if false
|
|---|
| 198 | install_files += [[ 'boot', kernel_disasm.full_path(), 'kernel.elf.disasm' ]]
|
|---|
| 199 | install_deps += [ kernel_disasm ]
|
|---|
| 200 | endif
|
|---|