Index: boot/Makefile.build
===================================================================
--- boot/Makefile.build	(revision 643640afa545800688f9f5810cb0ec66bf8aab85)
+++ boot/Makefile.build	(revision 98975a82755ce9aa9edd1fad289db15a79921dc8)
@@ -31,42 +31,4 @@
 include Makefile.common
 
-INCLUDES = -Igeneric/include -Iarch/$(KARCH)/include -Igenarch/include -I$(ROOT_PATH)/abi/arch/$(KARCH)/include -I$(ROOT_PATH)/abi/include
-OPTIMIZATION = 3
-
-DEFS = -DBOOT -DRELEASE=$(RELEASE) "-DCOPYRIGHT=$(COPYRIGHT)" "-DNAME=$(NAME)" -D__$(BITS)_BITS__ -D__$(ENDIANESS)__
-
-LDFLAGS = -Wl,--fatal-warnings,--warn-common $(EXTRA_LDFLAGS)
-
-COMMON_CFLAGS = $(INCLUDES) -O$(OPTIMIZATION) -imacros $(CONFIG_HEADER) \
-	-ffreestanding -nostdlib \
-	-fexec-charset=UTF-8 -finput-charset=UTF-8 -fno-common \
-	-fdebug-prefix-map=$(realpath $(ROOT_PATH))=. \
-	-Wa,--fatal-warnings
-
-GCC_CFLAGS = -Wall -Wextra -Wno-unused-parameter -Wmissing-prototypes \
-	-std=gnu99 -Werror-implicit-function-declaration -Wwrite-strings \
-	-pipe
-
-CLANG_CFLAGS = -Wall -Wextra -Wno-unused-parameter -Wmissing-prototypes \
-	-Werror-implicit-function-declaration -Wwrite-strings \
-	-pipe  -fno-stack-protector -fno-PIC
-
-ifeq ($(CONFIG_DEBUG),y)
-	COMMON_CFLAGS += -Werror
-endif
-
-ifeq ($(CONFIG_LINE_DEBUG),y)
-	COMMON_CFLAGS += -g
-endif
-
-ifeq ($(COMPILER),clang)
-	CFLAGS = $(COMMON_CFLAGS) $(CLANG_CFLAGS) $(EXTRA_CFLAGS)
-else
-	CFLAGS = $(COMMON_CFLAGS) $(GCC_CFLAGS) $(EXTRA_CFLAGS)
-endif
-
-OBJECTS := $(addsuffix .o,$(basename $(SOURCES)))
-DEPENDS := $(addsuffix .d,$(basename $(SOURCES)))
-
 ifeq ($(CONFIG_COMPRESSED_INIT),y)
 	COMPONENTS := $(addsuffix .gz, $(COMPONENTS))
@@ -80,29 +42,11 @@
 -include $(DEPENDS)
 
-$(BOOT_OUTPUT): $(RAW)
-	$(OBJCOPY) -O $(BFD_OUTPUT) $< $@
+depend: $(PRE_DEPEND)
 
-$(RAW): $(OBJECTS) $(LINK)
-	$(CC) $(CFLAGS) -Wl,-n $(LDFLAGS) -T $(LINK) -Wl,-M,-Map,$(MAP) -o $@ $(OBJECTS)
-
-$(LINK): | depend
-	$(CC) $(DEFS) $(CFLAGS) -D__ASSEMBLER__ -D__LINKER__ -E -x c $(LINK).in | grep -v "^\#" > $(LINK)
-
-%.o: %.s | depend
-	$(CC_JOB) -c -MD -MP $(DEFS) $(CFLAGS)
-
-%.o: %.S | depend
-	$(CC_JOB) -c -MD -MP $(DEFS) $(CFLAGS)
-
-%.o: %.c | depend
-	$(CC_JOB) -c -MD -MP $(DEFS) $(CFLAGS)
-
-depend: $(PRE_DEPEND)
+../image.boot: ../build/dist/boot/image.boot
+	cp $< $@
 
 %.gz: %
 	gzip -n -k -9 -f $<
-
-$(COMPS).tar: $(COMPONENTS)
-	tar --mtime='2032-01-01 00:00:00' --group=0 --owner=0 --no-acls --no-selinux --no-xattrs --format=ustar --transform 's/.*\///g' -cvf $@ $^
 
 $(COMPS).o: $(COMPS).tar
Index: boot/Makefile.common
===================================================================
--- boot/Makefile.common	(revision 643640afa545800688f9f5810cb0ec66bf8aab85)
+++ boot/Makefile.common	(revision 98975a82755ce9aa9edd1fad289db15a79921dc8)
@@ -54,12 +54,4 @@
 MKUIMAGE = $(TOOLS_PATH)/mkuimage.py
 
-ifeq ($(PRECHECK),y)
-	JOBFILE = $(TOOLS_PATH)/jobfile.py
-	# NOTE: You must not change the order of arguments.
-	CC_JOB = $(JOBFILE) $(JOB) $(CC) $< -o $@
-else
-	CC_JOB = $(CC) $< -o $@
-endif
-
 DISTROOT = distroot
 INITRD = initrd
@@ -102,5 +94,4 @@
 BOOT_OUTPUT ?= $(ROOT_PATH)/image.boot
 RAW ?= image.raw
-JOB ?= image.job
 MAP ?= image.map
 BUILD ?= Makefile.build
Index: boot/arch/abs32le/Makefile.inc
===================================================================
--- boot/arch/abs32le/Makefile.inc	(revision 643640afa545800688f9f5810cb0ec66bf8aab85)
+++ boot/arch/abs32le/Makefile.inc	(revision 98975a82755ce9aa9edd1fad289db15a79921dc8)
@@ -29,5 +29,4 @@
 BOOT_OUTPUT =
 RAW =
-JOB =
 MAP =
 BUILD = Makefile.empty
Index: boot/arch/amd64/Makefile.inc
===================================================================
--- boot/arch/amd64/Makefile.inc	(revision 643640afa545800688f9f5810cb0ec66bf8aab85)
+++ boot/arch/amd64/Makefile.inc	(revision 98975a82755ce9aa9edd1fad289db15a79921dc8)
@@ -27,6 +27,4 @@
 #
 
-POST_OUTPUT = $(ROOT_PATH)/image.iso
 BUILD = Makefile.empty
 POSTBUILD = Makefile.empty
-GRUB_LOADER = multiboot
Index: boot/arch/arm32/Makefile.inc
===================================================================
--- boot/arch/arm32/Makefile.inc	(revision 643640afa545800688f9f5810cb0ec66bf8aab85)
+++ boot/arch/arm32/Makefile.inc	(revision 98975a82755ce9aa9edd1fad289db15a79921dc8)
@@ -27,52 +27,4 @@
 #
 
-ifeq ($(MACHINE), gta02)
-	BOOT_OUTPUT = image.boot
-	POST_OUTPUT = $(ROOT_PATH)/uImage.bin
-	LADDR = 0x30008000
-	SADDR = 0x30008000
-	POSTBUILD = Makefile.uboot
-endif
-
-ifeq ($(MACHINE), $(filter $(MACHINE),beagleboardxm beaglebone))
-	BOOT_OUTPUT = image.boot
-	POST_OUTPUT = $(ROOT_PATH)/uImage.bin
-	LADDR = 0x80000000
-	SADDR = 0x80000000
-	POSTBUILD = Makefile.uboot
-endif
-
-ifeq ($(MACHINE), raspberrypi)
-	BOOT_OUTPUT = image.boot
-	POST_OUTPUT = $(ROOT_PATH)/uImage.bin
-	LADDR = 0x00008000
-	SADDR = 0x00008000
-	POSTBUILD = Makefile.uboot
-endif
-
-BFD_NAME = elf32-littlearm
-BFD_OUTPUT = $(BFD_NAME)
-
-BITS = 32
-ENDIANESS = LE
-EXTRA_CFLAGS = -march=$(subst _,-,$(PROCESSOR_ARCH)) -mno-unaligned-access
-
-SOURCES = \
-	arch/$(BARCH)/src/asm.S \
-	arch/$(BARCH)/src/eabi.S \
-	arch/$(BARCH)/src/main.c \
-	arch/$(BARCH)/src/mm.c \
-	arch/$(BARCH)/src/putchar.c \
-	$(COMPS).o \
-	genarch/src/division.c \
-	generic/src/memstr.c \
-	generic/src/printf_core.c \
-	generic/src/vprintf.c \
-	generic/src/printf.c \
-	generic/src/str.c \
-	generic/src/version.c \
-	generic/src/inflate.c \
-	generic/src/gzip.c \
-	generic/src/tar.c \
-	generic/src/kernel.c \
-	generic/src/payload.c
+BUILD = Makefile.empty
+POSTBUILD = Makefile.empty
Index: boot/arch/arm32/meson.build
===================================================================
--- boot/arch/arm32/meson.build	(revision 98975a82755ce9aa9edd1fad289db15a79921dc8)
+++ boot/arch/arm32/meson.build	(revision 98975a82755ce9aa9edd1fad289db15a79921dc8)
@@ -0,0 +1,51 @@
+BUILD = true
+
+boot_image_format = 'elf'
+
+if MACHINE == 'gta02'
+	LADDR = 0x30008000
+	SADDR = 0x30008000
+	POSTBUILD = 'uboot'
+	POST_OUTPUT = 'uImage.bin'
+endif
+
+if MACHINE == 'beagleboardxm' or MACHINE == 'beaglebone'
+	LADDR = 0x80000000
+	SADDR = 0x80000000
+	POSTBUILD = 'uboot'
+	POST_OUTPUT = 'uImage.bin'
+endif
+
+if MACHINE == 'raspberrypi'
+	LADDR = 0x00008000
+	SADDR = 0x00008000
+	POSTBUILD = 'uboot'
+	POST_OUTPUT = 'uImage.bin'
+endif
+
+if MACHINE == 'integratorcp'
+	POSTBUILD = 'raw'
+	POST_OUTPUT = 'image.boot'
+endif
+
+
+
+boot_src = files(
+	'src/asm.S',
+	'src/eabi.S',
+	'src/main.c',
+	'src/mm.c',
+	'src/putchar.c',
+	'../../genarch/src/division.c',
+	'../../generic/src/memstr.c',
+	'../../generic/src/printf_core.c',
+	'../../generic/src/vprintf.c',
+	'../../generic/src/printf.c',
+	'../../generic/src/str.c',
+	'../../generic/src/version.c',
+	'../../generic/src/inflate.c',
+	'../../generic/src/gzip.c',
+	'../../generic/src/tar.c',
+	'../../generic/src/kernel.c',
+	'../../generic/src/payload.c',
+)
Index: boot/meson.build
===================================================================
--- boot/meson.build	(revision 643640afa545800688f9f5810cb0ec66bf8aab85)
+++ boot/meson.build	(revision 98975a82755ce9aa9edd1fad289db15a79921dc8)
@@ -1,6 +1,160 @@
+subdir('arch' / BARCH)
 
-subdir('arch' / BARCH)
+if BUILD
+	# Order matters.
+	modules = [ 'boot/kernel.elf' ] + rd_init + [ 'boot/initrd.img' ]
+	moddeps = []
+	name_transform = ''
+
+	# Collect binary references in the correct order.
+	foreach m : modules
+		found = false
+		foreach bin : rd_init_binaries
+			if bin[1] == m
+				moddeps += bin[0]
+				found = true
+
+				_oldname = run_command(basename, bin[0].full_path(), check: true).stdout().strip()
+				_newname = run_command(basename, bin[1], check: true).stdout().strip()
+
+				name_transform += 's:.*/@0@:@1@:;'.format(_oldname, _newname)
+				break
+			endif
+		endforeach
+
+		if not found
+			error('Could not find dependency ' + m)
+		endif
+	endforeach
+
+
+	boot_include_dirs = include_directories(
+		'generic/include',
+		'arch'/BARCH/'include',
+		'genarch/include',
+		'../abi/arch'/BARCH/'include',
+		'../abi/include',
+	)
+
+	boot_defs = [
+		'-imacros', meson.source_root() / 'config.h',
+		'-D_HELENOS_SOURCE',
+		'-DBOOT',
+		'-DRELEASE=' + HELENOS_RELEASE,
+		'-DCOPYRIGHT=' + HELENOS_COPYRIGHT,
+		'-DNAME=' + HELENOS_NAME,
+		'-D__@0@_BITS__'.format(meson.get_cross_property('bits')),
+	]
+
+	boot_c_args = arch_boot_c_args + boot_defs + [
+		'-ffreestanding',
+	]
+
+	boot_link_args = arch_boot_link_args + [
+		'-nostdlib',
+		'-Wl,--nmagic',
+		'-T', meson.current_build_dir()/'_link.ld',
+		'-Wl,--no-gc-sections',
+	]
+
+	if cc.get_id() == 'clang'
+		boot_c_args += [
+			'-fno-stack-protector',
+			'-fno-PIC',
+		#	'-mllvm', '-asm-macro-max-nesting-depth=1000',
+		]
+	endif
+
+	# Preprocess linker script using C preprocessor.
+	boot_ldscript = custom_target('_link.ld',
+		input: 'arch'/KARCH/'_link.ld.in',
+		output: '_link.ld',
+		command: [
+			cc.cmd_array(),
+			boot_c_args,
+			'-I' + meson.current_source_dir()/'arch'/KARCH/'include',
+			'-D__ASSEMBLER__',
+			'-D__LINKER__',
+			'-E',
+			'-P',
+			'-x', 'c',
+			'@INPUT@',
+		],
+		capture: true,
+	)
+
+	# Create empty object file.
+	boot_empty_o = custom_target('empty.o',
+		output: 'empty.o',
+		command: [
+			cc.cmd_array(),
+			boot_c_args,
+			'-x', 'c',
+			'-c',
+			'-o', '@OUTPUT@',
+			'-',
+		],
+	)
+
+	boot_comps_tar = custom_target('components.tar',
+		input: moddeps,
+		output: 'components.tar',
+		command: [ tar, '--transform', name_transform ],
+	)
+
+	# Add .payload section to the empty object.
+	boot_comps_o = custom_target('components.o',
+		output: 'components.o',
+		input: [ boot_comps_tar, boot_empty_o ],
+		command: [
+			objcopy,
+			'--add-section', '.payload=@INPUT0@',
+			'@INPUT1@',
+			'@OUTPUT@',
+		],
+	)
+
+	boot_image_name = 'image.boot'
+	boot_image_map_path = meson.current_build_dir()/boot_image_name + '.map'
+
+	boot_elf = executable(boot_image_name + '.elf', boot_src, boot_comps_o,
+		include_directories: boot_include_dirs,
+		implicit_include_directories: false,
+		c_args: boot_c_args,
+		link_args: boot_c_args + boot_link_args + [
+			'-Wl,-Map,' + boot_image_map_path,
+		],
+		link_depends: boot_ldscript,
+		install: true,
+		install_dir: 'boot',
+		pie: false,
+	)
+
+	if boot_image_format == 'elf'
+		boot_image = boot_elf
+	endif
+
+	if boot_image_format == 'binary'
+		# Some platforms can't ELF.
+		boot_image = custom_target(boot_image_name,
+			input: boot_elf,
+			output: boot_image_name,
+			command: [ objcopy, '-O', 'binary', '@INPUT@', '@OUTPUT@' ],
+		)
+	endif
+endif
+
+if POSTBUILD == 'raw'
+	POST_INPUT = boot_image
+endif
 
 if POSTBUILD == 'grub'
 	subdir('grub')
 endif
+
+if POSTBUILD == 'uboot'
+	# TODO
+	error('unfinished')
+endif
+
+
Index: meson.build
===================================================================
--- meson.build	(revision 643640afa545800688f9f5810cb0ec66bf8aab85)
+++ meson.build	(revision 98975a82755ce9aa9edd1fad289db15a79921dc8)
@@ -19,4 +19,5 @@
 mkarray = find_program('tools/mkarray_for_meson.sh')
 objdump = find_program('objdump')
+objcopy = find_program('objcopy')
 sed = find_program('sed')
 unzip = find_program('unzip')
Index: meson/arch/arm32/meson.build
===================================================================
--- meson/arch/arm32/meson.build	(revision 643640afa545800688f9f5810cb0ec66bf8aab85)
+++ meson/arch/arm32/meson.build	(revision 98975a82755ce9aa9edd1fad289db15a79921dc8)
@@ -22,4 +22,7 @@
 arch_kernel_link_args = [ '-nostdlib', '-Wl,-z,max-page-size=0x1000' ]
 arch_uspace_link_args = [ '-nostdlib', '-lgcc', '-Wl,-z,max-page-size=0x1000' ]
+
+arch_boot_c_args = arch_kernel_c_args
+arch_boot_link_args = arch_kernel_link_args
 
 
Index: tools/ew.py
===================================================================
--- tools/ew.py	(revision 643640afa545800688f9f5810cb0ec66bf8aab85)
+++ tools/ew.py	(revision 98975a82755ce9aa9edd1fad289db15a79921dc8)
@@ -279,5 +279,5 @@
 		cmdline += ' -device scsi-cd,drive=cdrom'
 	elif cfg['image'] == 'image.boot':
-		cmdline += ' -kernel image.boot'
+		cmdline += ' -kernel build/image.boot'
 	else:
 		cmdline += ' ' + cfg['image']
