Debian Codegraph @

src/boot/meson.build

raw
1 # SPDX-License-Identifier: LGPL-2.1-or-later
2
3 efi_config_h_dir = meson.current_build_dir()
4 efi_addon = []
5
6 libefitest = static_library(
7 'efitest',
8 files(
9 'bcd.c',
10 'chid.c',
11 'efi-string.c',
12 ),
13 build_by_default : false,
14 include_directories : [
15 include_directories('.'),
16 basic_includes,
17 ],
18 implicit_include_directories : false,
19 dependencies : userspace)
20
21 efitest_base = {
22 'link_with' : [
23 libefitest,
24 libshared,
25 ],
26 }
27 efi_test_template = test_template + efitest_base
28 efi_fuzz_template = fuzz_template + efitest_base
29
30 if conf.get('ENABLE_UKIFY') == 1
31 test_hwids_section_c = custom_target(
32 input : ['hwids/test/device1.json', 'hwids/test/device2.json', 'hwids/test/device3.json', 'hwids/test/device4.json'],
33 output : 'test-hwids-section.c',
34 command : [files('generate-hwids-section.py'),
35 meson.current_source_dir()/'hwids/test'],
36 capture : true,
37 build_by_default : want_tests != 'false')
38 install_subdir('hwids',
39 install_dir : libexecdir / 'boot/',
40 exclude_directories : ['test'])
41 else
42 test_hwids_section_c = []
43 endif
44
45 generated_sources += test_hwids_section_c
46
47 executables += [
48 efi_test_template + {
49 'sources' : files('test-bcd.c'),
50 'dependencies' : libzstd_cflags,
51 'conditions' : ['ENABLE_BOOTLOADER', 'HAVE_ZSTD'],
52 },
53 efi_test_template + {
54 'sources' : files('test-efi-string.c'),
55 'conditions' : ['ENABLE_BOOTLOADER'],
56 },
57 efi_test_template + {
58 'sources' : files('test-chid-match.c') +
59 test_hwids_section_c,
60 'conditions' : ['ENABLE_BOOTLOADER', 'ENABLE_UKIFY'],
61 },
62 efi_fuzz_template + {
63 'sources' : files('fuzz-bcd.c'),
64 },
65 efi_fuzz_template + {
66 'sources' : files('fuzz-efi-string.c'),
67 },
68 efi_fuzz_template + {
69 'sources' : files('fuzz-efi-osrel.c'),
70 },
71 efi_fuzz_template + {
72 'sources' : files('fuzz-efi-printf.c'),
73 },
74 ]
75
76 if conf.get('ENABLE_BOOTLOADER') != 1
77 subdir_done()
78 endif
79
80 efi_conf = configuration_data()
81
82 # import several configs from userspace
83 foreach name : ['HAVE_WARNING_ZERO_LENGTH_BOUNDS',
84 'HAVE_WARNING_ZERO_AS_NULL_POINTER_CONSTANT']
85 efi_conf.set(name, conf.get(name))
86 endforeach
87
88 foreach attr : possible_c_attributes
89 name = 'HAVE_ATTRIBUTE_' + attr.to_upper()
90 efi_conf.set(name, conf.get(name))
91 endforeach
92
93 efi_conf.set10('ENABLE_TPM', get_option('tpm'))
94
95 foreach ctype : ['color-normal', 'color-entry', 'color-highlight', 'color-edit']
96 c = get_option('efi-' + ctype).split(',')
97 efi_conf.set(ctype.underscorify().to_upper(), 'EFI_TEXT_ATTR(@0@, @1@)'.format(
98 'EFI_' + c[0].strip().underscorify().to_upper(),
99 'EFI_' + c[1].strip().underscorify().to_upper()))
100 endforeach
101
102 efi_conf.set_quoted('PROJECT_VERSION', project_major_version)
103 efi_conf.set_quoted('VERSION_TAG', version_tag)
104 efi_conf.set('PROJECT_URL', conf.get('PROJECT_URL'))
105 efi_conf.set_quoted('RELATIVE_SOURCE_PATH', relative_source_path)
106
107 if meson.is_cross_build() and get_option('sbat-distro') == 'auto'
108 warning('Auto detection of SBAT information not supported when cross-building, disabling SBAT.')
109 elif get_option('sbat-distro') != ''
110 efi_conf.set_quoted('SBAT_PROJECT', meson.project_name())
111 if get_option('sbat-distro-generation') < 1
112 error('SBAT Distro Generation must be a positive integer')
113 endif
114 efi_conf.set('SBAT_DISTRO_GENERATION', get_option('sbat-distro-generation'))
115 foreach sbatvar : [['sbat-distro', 'ID'],
116 ['sbat-distro-summary', 'NAME'],
117 ['sbat-distro-url', 'BUG_REPORT_URL']]
118 value = get_option(sbatvar[0])
119 if (value == '' or value == 'auto') and not meson.is_cross_build()
120 cmd = 'if [ -e /etc/os-release ]; then . /etc/os-release; else . /usr/lib/os-release; fi; echo $@0@'.format(sbatvar[1])
121 value = run_command(sh, '-c', cmd, check: true).stdout().strip()
122 endif
123 if value == ''
124 error('Required @0@ option not set and autodetection failed'.format(sbatvar[0]))
125 endif
126 efi_conf.set_quoted(sbatvar[0].underscorify().to_upper(), value)
127 endforeach
128
129 pkgname = get_option('sbat-distro-pkgname')
130 if pkgname == ''
131 pkgname = meson.project_name()
132 endif
133 efi_conf.set_quoted('SBAT_DISTRO_PKGNAME', pkgname)
134
135 pkgver = get_option('sbat-distro-version')
136 if pkgver == ''
137 # This is determined during build, not configuration, so we can't display it yet.
138 efi_conf.set('SBAT_DISTRO_VERSION', 'GIT_VERSION')
139 else
140 efi_conf.set_quoted('SBAT_DISTRO_VERSION', pkgver)
141 endif
142 endif
143
144 summary({
145 'UEFI architectures' : efi_arch + (efi_arch_alt == '' ? '' : ', ' + efi_arch_alt)
146 },
147 section : 'UEFI')
148
149 if efi_conf.get('SBAT_DISTRO', '') != ''
150 summary({
151 'SBAT distro' : efi_conf.get('SBAT_DISTRO'),
152 'SBAT distro generation' : efi_conf.get('SBAT_DISTRO_GENERATION'),
153 'SBAT distro version' : efi_conf.get('SBAT_DISTRO_VERSION'),
154 'SBAT distro summary' : efi_conf.get('SBAT_DISTRO_SUMMARY'),
155 'SBAT distro URL' : efi_conf.get('SBAT_DISTRO_URL')
156 },
157 section : 'UEFI')
158 endif
159
160 configure_file(
161 output : 'efi-config.h',
162 configuration : efi_conf)
163
164 ############################################################
165
166 efi_includes = [
167 include_directories('.'),
168 fundamental_include,
169 version_include,
170 ]
171
172 efi_c_args = [
173 '-DSD_BOOT=1',
174 '-ffreestanding',
175 '-fno-strict-aliasing',
176 '-fshort-wchar',
177 '-include', 'efi-config.h',
178 ]
179
180 efi_c_args += cc.get_supported_arguments(
181 '-fwide-exec-charset=UCS2',
182 # gcc docs says this is required for ms_abi to work correctly.
183 '-maccumulate-outgoing-args',
184 '-mstack-protector-guard=global',
185 )
186
187 # Debug information has little value in release builds as no normal human being knows
188 # how to attach a debugger to EFI binaries running on real hardware. Anyone who does
189 # certainly has the means to do their own dev build.
190 if get_option('mode') == 'developer' and get_option('debug')
191 efi_c_args += '-DEFI_DEBUG'
192 endif
193
194 if get_option('libc') == 'musl'
195 # To make musl not define wchar_t as int, rather than short.
196 efi_c_args += '-D__DEFINED_wchar_t'
197 endif
198
199 efi_c_ld_args = [
200 '-nostdlib',
201 '-static-pie',
202 '-Wl,--entry=efi_main',
203 '-Wl,--fatal-warnings',
204
205 # These flags should be passed by -static-pie, but for whatever reason the flag translation
206 # is not enabled on all architectures. Not passing `-static` would just allow the linker to
207 # use dynamic libraries, (which we can't/don't use anyway). But if `-pie` is missing and the
208 # gcc build does not default to `-pie` we get a regular (no-pie) binary that will be
209 # rightfully rejected by elf2efi. Note that meson also passes `-pie` to the linker driver,
210 # but it is overridden by our `-static-pie`. We also need to pass these directly to the
211 # linker as `-static`+`-pie` seem to get translated differently.
212 '-Wl,-static,-pie,--no-dynamic-linker,-z,text',
213
214 # EFI has 4KiB pages.
215 '-z', 'common-page-size=4096',
216 '-z', 'max-page-size=4096',
217
218 '-z', 'noexecstack',
219 '-z', 'relro',
220 '-z', 'separate-code',
221 ]
222
223 linker_sanity_code = 'void a(void) {}; void _start(void) { a(); }'
224 linker_sanity_args = ['-nostdlib', '-Wl,--fatal-warnings', '-fno-stack-protector']
225
226 # Check if libgcc is available and then use it. It is needed for gcc builds
227 # and for ia32 builds with clang.
228 if cc.links(linker_sanity_code,
229 name : 'libgcc is available',
230 args : [linker_sanity_args, '-lgcc'])
231 efi_c_ld_args += ['-lgcc']
232 endif
233
234 efi_c_ld_args += cc.get_supported_link_arguments(
235 # binutils >= 2.38
236 '-Wl,-z,nopack-relative-relocs',
237 )
238
239 # efi_c_args is explicitly passed to targets so that they can override distro-provided flags
240 # that should not be used for EFI binaries.
241 efi_disabled_c_args = cc.get_supported_arguments(
242 '-fcf-protection=none',
243 '-fno-asynchronous-unwind-tables',
244 '-fno-exceptions',
245 '-fno-unwind-tables',
246 )
247 efi_override_options = [
248 'b_coverage=false',
249 'b_pgo=off',
250 ]
251
252 if get_option('b_sanitize') == 'undefined'
253 efi_disabled_c_args += cc.get_supported_arguments('-fno-sanitize-link-runtime')
254 else
255 efi_disabled_c_args += cc.get_supported_arguments('-fno-sanitize=all')
256 efi_override_options += 'b_sanitize=none'
257 endif
258
259 efi_c_args += efi_disabled_c_args
260 efi_c_ld_args += efi_disabled_c_args
261
262 if cc.get_id() == 'clang'
263 # clang is too picky sometimes.
264 efi_c_args += '-Wno-unused-command-line-argument'
265 efi_c_ld_args += '-Wno-unused-command-line-argument'
266 endif
267
268 efi_arch_c_args = {
269 'aarch64' : ['-mgeneral-regs-only'],
270 'arm' : ['-mgeneral-regs-only'],
271 # Until -mgeneral-regs-only is supported in LoongArch, use the following option instead:
272 'loongarch64' : ['-mno-lsx', '-mno-lasx'],
273 # Pass -m64/32 explicitly to make building on x32 work.
274 'x86_64' : ['-m64', '-march=x86-64', '-mno-red-zone', '-mgeneral-regs-only'],
275 'x86' : ['-m32', '-march=i686', '-mgeneral-regs-only', '-malign-double'],
276 }
277 efi_arch_c_ld_args = {
278 # libgcc is not compiled with -fshort-wchar, but it does not use it anyways,
279 # so it's fine to link against it.
280 'arm' : cc.get_supported_link_arguments('-Wl,--no-wchar-size-warning'),
281 'x86_64' : ['-m64'],
282 'x86' : ['-m32'],
283 }
284
285 if not cc.links(linker_sanity_code,
286 name : 'linker supports -static-pie',
287 args : [linker_sanity_args, '-static-pie'])
288 error('Linker does not support -static-pie.')
289 endif
290
291 # https://github.com/llvm/llvm-project/issues/67152
292 if not cc.links(linker_sanity_code,
293 name : 'linker supports LTO with -nostdlib',
294 args : [linker_sanity_args, '-flto'])
295 efi_c_args += '-fno-lto'
296 efi_c_ld_args += '-fno-lto'
297 endif
298
299 # https://github.com/llvm/llvm-project/issues/61101
300 if efi_cpu_family_alt == 'x86' and not cc.links(linker_sanity_code,
301 name : 'linker supports LTO with -nostdlib (x86)',
302 args : [linker_sanity_args, '-flto', '-m32'])
303 efi_arch_c_args += { 'x86' : efi_arch_c_args['x86'] + '-fno-lto' }
304 efi_arch_c_ld_args += { 'x86' : efi_arch_c_ld_args['x86'] + '-fno-lto' }
305 endif
306
307 ############################################################
308
309 libefi_sources = files(
310 'chid.c',
311 'console.c',
312 'cpio.c',
313 'device-path-util.c',
314 'devicetree.c',
315 'drivers.c',
316 'edid.c',
317 'efi-efivars.c',
318 'efi-firmware.c',
319 'efi-log.c',
320 'efi-string.c',
321 'export-vars.c',
322 'graphics.c',
323 'hii.c',
324 'initrd.c',
325 'measure.c',
326 'measure-smbios.c',
327 'part-discovery.c',
328 'pe.c',
329 'random-seed.c',
330 'secure-boot.c',
331 'shim.c',
332 'smbios.c',
333 'sysfail.c',
334 'ticks.c',
335 'url-discovery.c',
336 'util.c',
337 'vmm.c',
338 )
339
340 systemd_boot_sources = files(
341 'boot.c',
342 'line-edit.c',
343 )
344
345 stub_sources = files(
346 'boot-secret.c',
347 'linux.c',
348 'splash.c',
349 'stub.c',
350 )
351
352 addon_sources = files(
353 'addon.c',
354 )
355
356 if get_option('b_sanitize') == 'undefined'
357 libefi_sources += files('ubsan.c')
358 endif
359
360 if host_machine.cpu_family() in ['x86', 'x86_64']
361 stub_sources += files('linux_x86.c')
362 endif
363
364 # BCD parser only makes sense on arches that Windows supports.
365 if host_machine.cpu_family() in ['aarch64', 'arm', 'x86_64', 'x86']
366 systemd_boot_sources += files('bcd.c')
367 endif
368
369 sources += libefi_sources
370 sources += systemd_boot_sources
371 sources += stub_sources
372 sources += addon_sources
373
374 boot_targets = []
375 efi_elf_binaries = []
376 efi_archspecs = [
377 {
378 'arch' : efi_arch,
379 'c_args' : [
380 efi_c_args,
381 '-DEFI_MACHINE_TYPE_NAME="' + efi_arch + '"',
382 efi_arch_c_args.get(host_machine.cpu_family(), []),
383 ],
384 'link_args' : [
385 efi_c_ld_args,
386 efi_arch_c_ld_args.get(host_machine.cpu_family(), []),
387 ],
388 },
389 ]
390 if efi_arch_alt != ''
391 efi_archspecs += {
392 'arch' : efi_arch_alt,
393 'c_args' : [
394 efi_c_args,
395 '-DEFI_MACHINE_TYPE_NAME="' + efi_arch_alt + '"',
396 efi_arch_c_args.get(efi_cpu_family_alt, []),
397 ],
398 'link_args' : [
399 efi_c_ld_args,
400 efi_arch_c_ld_args.get(efi_cpu_family_alt, []),
401 ],
402 }
403 endif
404
405 foreach archspec : efi_archspecs
406 libefi = static_library(
407 'efi' + archspec['arch'],
408 fundamental_sources,
409 libefi_sources,
410 version_h,
411 include_directories : efi_includes,
412 implicit_include_directories : false,
413 c_args : archspec['c_args'],
414 gnu_symbol_visibility : 'hidden',
415 override_options : efi_override_options,
416 pic : true)
417
418 kwargs = {
419 'include_directories' : efi_includes,
420 'implicit_include_directories' : false,
421 'c_args' : archspec['c_args'],
422 'link_args' : archspec['link_args'],
423 'gnu_symbol_visibility' : 'hidden',
424 'override_options' : efi_override_options,
425 'pie' : true,
426 }
427
428 efi_elf_binaries += executable(
429 'systemd-boot' + archspec['arch'],
430 sources : [systemd_boot_sources, version_h],
431 link_with : libefi,
432 name_suffix : 'elf',
433 kwargs : kwargs)
434
435 efi_elf_binaries += executable(
436 'linux' + archspec['arch'],
437 sources : [stub_sources, version_h],
438 link_with : libefi,
439 name_suffix : 'elf.stub',
440 kwargs : kwargs)
441
442 efi_elf_binaries += executable(
443 'addon' + archspec['arch'],
444 sources : [addon_sources, version_h],
445 name_suffix : 'elf.stub',
446 kwargs : kwargs)
447 endforeach
448
449 foreach efi_elf_binary : efi_elf_binaries
450 name = efi_elf_binary.name()
451 name += name.startswith('systemd-boot') ? '.efi' : '.efi.stub'
452
453 # For the addon, given it's empty, we need to explicitly reserve space in the header to account for
454 # the sections that ukify will add.
455 if name.startswith('linux')
456 minimum_sections = get_option('efi-stub-extra-sections')
457 elif name.startswith('addon')
458 minimum_sections = get_option('efi-addon-extra-sections')
459 else
460 minimum_sections = 0
461 endif
462
463 exe = custom_target(
464 output : name,
465 input : efi_elf_binary,
466 install : true,
467 install_dir : bootlibdir,
468 install_tag : 'systemd-boot',
469 command : [
470 elf2efi_py,
471 f'--version-major=@project_major_version@',
472 f'--version-minor=@project_minor_version@',
473 '--efi-major=1',
474 '--efi-minor=1',
475 '--subsystem=10',
476 f'--minimum-sections=@minimum_sections@',
477 '--copy-sections=.sbat,.sdmagic,.osrel',
478 '@INPUT@',
479 '@OUTPUT@',
480 ])
481 boot_targets += exe
482 if name.startswith('linux')
483 boot_stubs += exe
484 endif
485
486 # This is supposed to match exactly one time
487 if name == f'addon@[email protected]'
488 efi_addon = [exe]
489 endif
490
491 test(f'check-alignment-@name@',
492 check_efi_alignment_py,
493 args : exe,
494 suite : 'boot')
495 endforeach
496
497 alias_target('systemd-boot', boot_targets)