Skip to content

Commit

Permalink
Makefile: Add a m1n1.bin target that builds a raw binary
Browse files Browse the repository at this point in the history
This is to be used for the non-macho boot support in 12.1+ kmutil.

Signed-off-by: Hector Martin <marcan@marcan.st>
  • Loading branch information
marcan committed Dec 17, 2021
1 parent bedcc90 commit 0d4fb00
Show file tree
Hide file tree
Showing 2 changed files with 163 additions and 3 deletions.
15 changes: 12 additions & 3 deletions Makefile
Expand Up @@ -35,7 +35,7 @@ CFLAGS := -O2 -Wall -g -Wundef -Werror=strict-prototypes -fno-common -fno-PIE \
-fno-stack-protector -mgeneral-regs-only -mstrict-align -march=armv8.2-a \
$(EXTRA_CFLAGS)

LDFLAGS := -T m1n1.ld -EL -maarch64elf --no-undefined -X -Bsymbolic \
LDFLAGS := -EL -maarch64elf --no-undefined -X -Bsymbolic \
-z notext --no-apply-dynamic-relocs --orphan-handling=warn \
-z nocopyreloc --gc-sections -pie

Expand Down Expand Up @@ -95,11 +95,12 @@ DTBS := $(patsubst %.dts,build/dtb/%.dtb,$(DTS))

NAME := m1n1
TARGET := m1n1.macho
TARGET_RAW := m1n1.bin

DEPDIR := build/.deps

.PHONY: all clean format update_tag
all: build/$(TARGET) $(DTBS)
all: build/$(TARGET) build/$(TARGET_RAW) $(DTBS)
clean:
rm -rf build/*
format:
Expand Down Expand Up @@ -131,12 +132,20 @@ build/%.o: src/%.c

build/$(NAME).elf: $(BUILD_OBJS) m1n1.ld
@echo " LD $@"
@$(LD) $(LDFLAGS) -o $@ $(BUILD_OBJS)
@$(LD) -T m1n1.ld $(LDFLAGS) -o $@ $(BUILD_OBJS)

build/$(NAME)-raw.elf: $(BUILD_OBJS) m1n1-raw.ld
@echo " LDRAW $@"
@$(LD) -T m1n1-raw.ld $(LDFLAGS) -o $@ $(BUILD_OBJS)

build/$(NAME).macho: build/$(NAME).elf
@echo " MACHO $@"
@$(OBJCOPY) -O binary --strip-debug $< $@

build/$(NAME).bin: build/$(NAME)-raw.elf
@echo " RAW $@"
@$(OBJCOPY) -O binary --strip-debug $< $@

update_tag:
@echo "#define BUILD_TAG \"$$(git describe --always --dirty)\"" > build/build_tag.tmp
@cmp -s build/build_tag.h build/build_tag.tmp 2>/dev/null || \
Expand Down
151 changes: 151 additions & 0 deletions m1n1-raw.ld
@@ -0,0 +1,151 @@
ENTRY(_start)

_stack_size = 0x20000;

_max_payload_size = 64*1024*1024;

/* We are actually relocatable */
. = 0;

PHDRS
{
hdr PT_LOAD;
text PT_LOAD;
rodata PT_LOAD;
data PT_LOAD;
}

SECTIONS {
_base = .;

_text_start = .;
.init : ALIGN(0x4000) {
*(.init)
*(.init.*)
} :text
.text : ALIGN(0x4000) {
*(.text)
*(.text.*)
. = ALIGN(8);
*(.got.plt)
. = ALIGN(0x4000);
} :text
_text_size = . - _text_start;
.rodata : ALIGN(0x4000) {
*(.rodata)
*(.rodata.*)
. = ALIGN(8);
} :rodata
.rela.dyn : {
_rela_start = .;
*(.rela)
*(.rela.text)
*(.rela.got)
*(.rela.plt)
*(.rela.bss)
*(.rela.ifunc)
*(.rela.text.*)
*(.rela.data)
*(.rela.data.*)
*(.rela.dyn)
_rela_end = .;
. = ALIGN(0x4000);
} :rodata
_rodata_end = .;
_data_start = .;
.data : ALIGN(0x4000) {
*(.data)
*(.data.*)
. = ALIGN(8);
_got_start = .;
*(.got)
_got_end = .;
. = ALIGN(0x4000);
_file_end = .;
} :data
.bss : ALIGN(0x4000) {
_bss_start = .;
*(.bss)
*(.bss.*)
*(.dynbss)
*(COMMON)
_bss_end = .;
} : data
.stack : ALIGN(0x4000) {
PROVIDE(_stack_top = .);
. += _stack_size - 8;
QUAD(0x544f424b43415453);

This comment has been minimized.

Copy link
@konradybcio

konradybcio Feb 6, 2024

Was this some breadcrumb used for debugging/validation, or is this actually meaningful?

This comment has been minimized.

Copy link
@marcan

marcan Feb 7, 2024

Author Member

It is meaningful: it forces the stack section to be allocated in the raw binary copy, which then allows the payloads to begin after the stack section (immediately after this word). Without this, they would not be placed correctly when appended.

IIRC I also started using it in the installer to locate the payload section for preservation across updates, so it has double meaning now.

PROVIDE(_stack_bot = .);
} :data
ASSERT(ALIGN(0x4000) == ., "Stack size is not aligned!")
_data_size = . - _data_start;
_end = .;
_payload_start = .;
_payload_end = . + _max_payload_size;

.symtab 0 : { *(.symtab) }
.strtab 0 : { *(.strtab) }
.shstrtab 0 : { *(.shstrtab) }

/DISCARD/ : {
*(.discard)
*(.discard.*)
*(.interp .dynamic)
*(.dynsym .dynstr .hash .gnu.hash)
*(.eh_frame)
*(.gnu.version*)
*(.note*)
*(.comment*)
}

.empty (NOLOAD) : {
*(.plt) *(.plt.*) *(.iplt) *(.igot)
*(.data.rel.ro)
}
ASSERT(SIZEOF(.empty) == 0, "Unexpected sections detected!")

.got.plt (NOLOAD) : {
*(.got.plt)
}
ASSERT(SIZEOF(.got.plt) == 0 || SIZEOF(.got.plt) == 0x18, "Unexpected GOT PLT detected!")

.stab 0 : { *(.stab) }
.stabstr 0 : { *(.stabstr) }
.stab.excl 0 : { *(.stab.excl) }
.stab.exclstr 0 : { *(.stab.exclstr) }
.stab.index 0 : { *(.stab.index) }
.stab.indexstr 0 : { *(.stab.indexstr) }
.comment 0 : { *(.comment) }
.debug 0 : { *(.debug) }
.line 0 : { *(.line) }
.debug_srcinfo 0 : { *(.debug_srcinfo) }
.debug_sfnames 0 : { *(.debug_sfnames) }
.debug_aranges 0 : { *(.debug_aranges) }
.debug_pubnames 0 : { *(.debug_pubnames) }
.debug_info 0 : { *(.debug_info .gnu.linkonce.wi.*) }
.debug_abbrev 0 : { *(.debug_abbrev) }
.debug_line 0 : { *(.debug_line) }
.debug_frame 0 : { *(.debug_frame) }
.debug_str 0 : { *(.debug_str) }
.debug_loc 0 : { *(.debug_loc) }
.debug_macinfo 0 : { *(.debug_macinfo) }
.debug_weaknames 0 : { *(.debug_weaknames) }
.debug_funcnames 0 : { *(.debug_funcnames) }
.debug_typenames 0 : { *(.debug_typenames) }
.debug_varnames 0 : { *(.debug_varnames) }
.debug_pubtypes 0 : { *(.debug_pubtypes) }
.debug_ranges 0 : { *(.debug_ranges) }
.debug_types 0 : { *(.debug_types) }
.debug_addr 0 : { *(.debug_addr) }
.debug_line_str 0 : { *(.debug_line_str) }
.debug_loclists 0 : { *(.debug_loclists) }
.debug_macro 0 : { *(.debug_macro) }
.debug_names 0 : { *(.debug_names) }
.debug_rnglists 0 : { *(.debug_rnglists) }
.debug_str_offsets 0 : { *(.debug_str_offsets) }
.debug_sup 0 : { *(.debug_sup) }
}

PROT_READ = 0x01;
PROT_WRITE = 0x02;
PROT_EXECUTE = 0x04;

0 comments on commit 0d4fb00

Please sign in to comment.