From 088912e2fcbfcb67cabe4838a5cb30b40b5955a9 Mon Sep 17 00:00:00 2001 From: Rui Ueyama Date: Fri, 24 Dec 2021 20:15:07 +0900 Subject: [PATCH] [ELF] Create less number of PT_NOTE segments --- elf/passes.cc | 18 ++++++++++-------- test/elf/note2.sh | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 8 deletions(-) create mode 100755 test/elf/note2.sh diff --git a/elf/passes.cc b/elf/passes.cc index 334d674e..659cfc4a 100644 --- a/elf/passes.cc +++ b/elf/passes.cc @@ -759,23 +759,25 @@ void clear_padding(Context &ctx) { // alloc writable non-RELRO bss // nonalloc // section header +// +// .note sections are sorted by their alignments. template i64 get_section_rank(Context &ctx, Chunk *chunk) { u64 type = chunk->shdr.sh_type; u64 flags = chunk->shdr.sh_flags; if (chunk == ctx.ehdr.get()) - return -4; + return 0; if (chunk == ctx.phdr.get()) - return -3; + return 1; if (chunk == ctx.interp.get()) - return -2; + return 2; if (type == SHT_NOTE && (flags & SHF_ALLOC)) - return -1; + return (1 << 10) + chunk->shdr.sh_addralign; if (chunk == ctx.shdr.get()) - return 1 << 6; + return 1 << 30; if (!(flags & SHF_ALLOC)) - return 1 << 5; + return (1 << 30) - 1; bool writable = (flags & SHF_WRITE); bool exec = (flags & SHF_EXECINSTR); @@ -783,8 +785,8 @@ i64 get_section_rank(Context &ctx, Chunk *chunk) { bool relro = is_relro(ctx, chunk); bool is_bss = (type == SHT_NOBITS); - return (writable << 4) | (exec << 3) | (!tls << 2) | - (!relro << 1) | is_bss; + return (1 << 20) | (writable << 19) | (exec << 18) | (!tls << 17) | + (!relro << 16) | (is_bss << 15); } // Returns the smallest number n such that diff --git a/test/elf/note2.sh b/test/elf/note2.sh new file mode 100755 index 00000000..bfb15a61 --- /dev/null +++ b/test/elf/note2.sh @@ -0,0 +1,38 @@ +#!/bin/bash +export LANG= +set -e +cd $(dirname $0) +mold=`pwd`/../../mold +echo -n "Testing $(basename -s .sh $0) ... " +t=$(pwd)/../../out/test/elf/$(basename -s .sh $0) +mkdir -p $t + +cat < $t/log +fgrep -q '01 .note.b' $t/log +fgrep -q '02 .note.a .note.c' $t/log + +echo OK