Skip to content

Commit 04f10eb

Browse files
committed
build: set max-page-size linker flag to match target page size
ELF binaries embed the maximum page size used during linking in the PT_LOAD segment alignment. If this value is larger than the actual system page size, the kernel will still load the binary but wastes memory due to over-alignment. If it is smaller, the binary may fail to load or cause incorrect behavior with features like RELRO. Most architectures use 4KB pages, but loongarch64 uses 16KB pages (CONFIG_PAGE_SIZE_16KB). Explicitly pass -z max-page-size to the linker so that binaries are built with the correct page alignment for the target, avoiding issues with RELRO segment padding and ensuring correct memory mapping on the target system. Before binutils 2.39 RELRO sections were padded to common page size which is 4K on MIPS, with binutils 3.29 the padding was changed to max page size which is 64K on MIPS. With this change we will pad them to 4K again. This reduces the size of user space binaries in OpenWrt. The size of the uncompressed root file system of a mips malta be build decreased by about 20%. old file size: ``` $ du -s build_dir/target-mips_24kc_musl/root-malta/ 15844 build_dir/target-mips_24kc_musl/root-malta/ ``` New file size: ``` $ du -s build_dir/target-mips_24kc_musl/root-malta/ 12564 build_dir/target-mips_24kc_musl/root-malta/ ``` The size of the image did not decrease much because of the strong compression used by OpenWrt. Link: openwrt/openwrt#22800 Signed-off-by: Hauke Mehrtens <hauke@hauke-m.de>
1 parent 1c0aec0 commit 04f10eb

1 file changed

Lines changed: 9 additions & 0 deletions

File tree

include/package.mk

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,15 @@ ifdef CONFIG_USE_MOLD
6161
endif
6262
endif
6363

64+
# loongarch64 sets CONFIG_PAGE_SIZE_16KB, all other targets set CONFIG_PAGE_SIZE_4KB only.
65+
ifeq ($(ARCH),loongarch64)
66+
TARGET_CFLAGS += -Wl,-z,max-page-size=16384
67+
TARGET_LDFLAGS += -zmax-page-size=16384
68+
else
69+
TARGET_CFLAGS += -Wl,-z,max-page-size=4096
70+
TARGET_LDFLAGS += -zmax-page-size=4096
71+
endif
72+
6473
include $(INCLUDE_DIR)/hardening.mk
6574
include $(INCLUDE_DIR)/prereq.mk
6675
include $(INCLUDE_DIR)/unpack.mk

0 commit comments

Comments
 (0)