1
1
mirror of https://github.com/rui314/mold.git synced 2024-09-21 01:47:11 +03:00

[ELF] Create less number of PT_NOTE segments

This commit is contained in:
Rui Ueyama 2021-12-24 20:15:07 +09:00
parent 29d956a7b0
commit 088912e2fc
2 changed files with 48 additions and 8 deletions

View File

@ -759,23 +759,25 @@ void clear_padding(Context<E> &ctx) {
// alloc writable non-RELRO bss
// nonalloc
// section header
//
// .note sections are sorted by their alignments.
template <typename E>
i64 get_section_rank(Context<E> &ctx, Chunk<E> *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<E> &ctx, Chunk<E> *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

38
test/elf/note2.sh Executable file
View File

@ -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 <<EOF | cc -o $t/a.o -c -x assembler -
.section .note.a, "a", @note
.p2align 3
.quad 42
EOF
cat <<EOF | cc -o $t/b.o -c -x assembler -
.section .note.b, "a", @note
.p2align 2
.quad 42
EOF
cat <<EOF | cc -o $t/c.o -c -x assembler -
.section .note.c, "a", @note
.p2align 3
.quad 42
EOF
cat <<EOF | cc -o $t/d.o -c -xc -
int main() {}
EOF
$mold -static -o $t/exe $t/a.o $t/b.o $t/c.o $t/d.o
readelf --segments $t/exe > $t/log
fgrep -q '01 .note.b' $t/log
fgrep -q '02 .note.a .note.c' $t/log
echo OK