Silence compiler when accessing low memory addresses
[helenos.git] / kernel / meson.build
blob286ddc29285859d593af1a4b6e1c252247f96c39
2 # Copyright (c) 2005 Martin Decky
3 # All rights reserved.
5 # Redistribution and use in source and binary forms, with or without
6 # modification, are permitted provided that the following conditions
7 # are met:
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.
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.
30 arch_src = []
32 # Fills arch_src.
33 subdir('arch' / KARCH)
35 # Defines genarch_src.
36 subdir('genarch')
38 # Defines generic_src, instrumentable_src.
39 subdir('generic')
41 # Defines test_src
42 subdir('test')
44 ## Cross-platform assembly to start a symtab.data section
46 symtab_section = '.section symtab.data, "a", ' + atsign + 'progbits;'
48 kernel_include_dirs = include_directories(
49         'generic/include',
50         'genarch/include',
51         'arch' / KARCH / 'include',
52         '..' / 'abi' / 'arch' / KARCH / 'include',
53         '..' / 'abi' / 'include',
54         'test',
57 kernel_defs = [
58         '-imacros', meson.build_root() / 'config.h',
59         '-D_HELENOS_SOURCE',
60         '-DKERNEL',
61         '-DHELENOS_RELEASE=' + HELENOS_RELEASE,
62         '-DHELENOS_COPYRIGHT=' + HELENOS_COPYRIGHT,
63         '-DHELENOS_CODENAME=' + HELENOS_CODENAME,
64         '-D__@0@_BITS__'.format(meson.get_cross_property('bits')),
67 # Preprocess linker script using C preprocessor.
68 kernel_ldscript = custom_target('_link.ld',
69         input: 'arch' / KARCH / '_link.ld.in',
70         output: '_link.ld',
71         command: [
72                 cc.cmd_array(),
73                 arch_kernel_c_args,
74                 kernel_defs,
75                 '-I' + meson.current_source_dir() / 'arch' / KARCH / 'include',
76                 '-D__ASSEMBLER__',
77                 '-D__LINKER__',
78                 '-E',
79                 '-P',
80                 '-x', 'c',
81                 '@INPUT@',
82         ],
83         capture: true,
84         build_by_default: true,
87 kernel_link_args = arch_kernel_link_args + [
88         '-Wl,--nmagic',
89         '-T', meson.current_build_dir() / '_link.ld',
92 if CONFIG_LTO
93         kernel_link_args += [ '-flto' ]
94 endif
96 if CONFIG_STRIP_BINARIES
97         # TODO: do this after disassembling
98         kernel_link_args += [ '-s' ]
99 endif
101 kernel_c_args = arch_kernel_c_args + kernel_defs + [
102         '-ffreestanding',
104         cc.get_supported_arguments([
105                 # TODO: remove this flag
106                 '-Wno-cast-function-type',
108                 # When accessing specific memory addresses that are below
109                 # normal page size, the compiler may assume that we actually
110                 # dereferenced NULL pointer and warns us about that.
111                 # But in kernel we often need to access these addresses
112                 # directly hence we need to ignore these warnings.
113                 #
114                 # TODO: might make more sense to disable this selectively
115                 # in specific files (or better yet, for specific lines).
116                 '--param=min-pagesize=0',
117         ]),
120 if CONFIG_LTO
121         kernel_c_args += [ '-flto' ]
122 endif
124 if cc.get_id() == 'clang'
125         kernel_c_args += [
126                 '-fno-stack-protector',
127                 '-fno-PIC',
128                 '-mllvm', '-asm-macro-max-nesting-depth=1000',
129         ]
130 endif
132 instrumentables = static_library('instrumentables', instrumentable_src,
133         include_directories: kernel_include_dirs,
134         implicit_include_directories: false,
135         c_args: kernel_c_args + (CONFIG_TRACE ? [ '-finstrument-functions' ] : []),
136         pic: false,
139 noninstrumentables = static_library('noninstrumentables', arch_src, genarch_src, generic_src, test_src,
140         include_directories: kernel_include_dirs,
141         implicit_include_directories: false,
142         c_args: kernel_c_args,
143         pic: false,
146 all_kernel_objects = [ instrumentables, noninstrumentables ]
148 # We iterate the build several times to get symbol table right.
149 # Three times is sufficient to get correct even symbols after symtab.
151 if CONFIG_SYMTAB
152         # Iterate build three times.
153         iterations = [ 1, 2, 3 ]
155         # Generates symbol table information as an object file.
156         genmap = find_program('tools/genmap.py')
158         # Symbol table dump needed for genmap.
159         kernel_syms = custom_target('kernel_syms.txt',
160                 input: all_kernel_objects,
161                 output: 'kernel_syms.txt',
162                 command: [ objdump, '-t', '@INPUT@' ],
163                 capture: true,
164         )
165 else
166         # Build just once.
167         iterations = [ 1 ]
168 endif
170 # Empty symbol map for first iteration.
171 kernel_map_S = custom_target('empty_map.S',
172         output: 'empty_map.S',
173         capture: true,
174         command: [ 'echo', kernel_as_prolog + symtab_section ],
177 foreach iter : iterations
178         is_last = (iter == iterations.length())
179         kernel_name = 'kernel.@0@.elf'.format(iter)
180         kernel_map_name = kernel_name + '.map'
181         kernel_map_path = meson.current_build_dir() / kernel_map_name
183         kernel_elf = executable(kernel_name, kernel_map_S,
184                 include_directories: kernel_include_dirs,
185                 implicit_include_directories: false,
186                 c_args: kernel_c_args,
187                 link_args: kernel_c_args + kernel_link_args + [
188                         '-Wl,-Map,' + kernel_map_path,
189                 ],
190                 link_depends: kernel_ldscript,
191                 link_whole: all_kernel_objects,
192                 pie: false,
193         )
195         # Generate symbol table if this is not the final iteration.
196         if not is_last
198                 # TODO: Teach kernel to read its own ELF symbol table and get rid of this nonsense.
199                 # Need to first make sure all architectures (even future ones with dumb bootloaders) can use ELF formatted kernel.
201                 kernel_map_bin = custom_target(kernel_map_name + '.bin',
202                         output: kernel_map_name + '.bin',
203                         input: [ kernel_elf, kernel_syms ],
204                         command: [ genmap, kernel_map_path, '@INPUT1@', '@OUTPUT@' ],
205                 )
207                 kernel_map_S_name = kernel_name + '.map.S'
209                 kernel_map_S = custom_target(kernel_map_S_name,
210                         input: kernel_map_bin,
211                         output: kernel_map_S_name,
212                         capture: true,
213                         command: [ 'echo', kernel_as_prolog + symtab_section + ' .incbin "@INPUT@"' ],
214                 )
215         endif
216 endforeach
218 rd_init_binaries += [[ kernel_elf, 'boot/kernel.elf' ]]
220 install_files += [[ 'boot', kernel_elf.full_path(), 'kernel.elf' ]]
221 install_deps += [ kernel_elf ]
223 kernel_disasm = custom_target('kernel.elf.disasm',
224         command: [ objdump, '-S', '@INPUT@' ],
225         input: kernel_elf,
226         output: 'kernel.elf.disasm',
227         capture: true,
228         build_by_default: true,
231 # TODO: Add configuration option for installing debug files
232 if false
233         install_files += [[ 'boot', kernel_disasm.full_path(), 'kernel.elf.disasm' ]]
234         install_deps += [ kernel_disasm ]
235 endif