From 90d174ac25932a1c6c6fba906a1482f129608ef4 Mon Sep 17 00:00:00 2001 From: yashwant Date: Mon, 5 Oct 2026 21:23:29 +0530 Subject: [PATCH] [ELD] Avoid assigning addresses to headers outside the layout Signed-off-by: yashwant --- lib/Target/GNULDBackend.cpp | 8 +++-- .../HeadersWithoutOutputSection.test | 36 +++++++++++++++++++ 2 files changed, 41 insertions(+), 3 deletions(-) create mode 100644 test/Common/standalone/linkerscript/PHDRS/HeadersWithoutOutputSection/HeadersWithoutOutputSection.test diff --git a/lib/Target/GNULDBackend.cpp b/lib/Target/GNULDBackend.cpp index 0add7fd574..485e8e9e22 100644 --- a/lib/Target/GNULDBackend.cpp +++ b/lib/Target/GNULDBackend.cpp @@ -5420,12 +5420,14 @@ bool GNULDBackend::allocateHeaders() { uint64_t ModifiedAddr = llvm::alignDown(VAddr.value() - headerSize, abiPageSize()); uint64_t ModifiedPAddr = PAddr.value() + (ModifiedAddr - VAddr.value()); - if (m_ehdr) { + if (isEhdrInLayout()) { m_ehdr->setAddr(ModifiedAddr); m_ehdr->setPaddr(ModifiedPAddr); } - m_phdr->setAddr(ModifiedAddr + getOneEhdrSize()); - m_phdr->setPaddr(ModifiedPAddr + getOneEhdrSize()); + if (isPhdrInLayout()) { + m_phdr->setAddr(ModifiedAddr + getOneEhdrSize()); + m_phdr->setPaddr(ModifiedPAddr + getOneEhdrSize()); + } return true; } return false; diff --git a/test/Common/standalone/linkerscript/PHDRS/HeadersWithoutOutputSection/HeadersWithoutOutputSection.test b/test/Common/standalone/linkerscript/PHDRS/HeadersWithoutOutputSection/HeadersWithoutOutputSection.test new file mode 100644 index 0000000000..2ffbcda7d3 --- /dev/null +++ b/test/Common/standalone/linkerscript/PHDRS/HeadersWithoutOutputSection/HeadersWithoutOutputSection.test @@ -0,0 +1,36 @@ +# Headers omitted from the layout have no output section. Allocating space for +# the other header must not try to assign addresses to the omitted header. +# RUN: split-file %s %t +# RUN: %clang %clangg0opts -c %t/input.c -o %t.o +# RUN: %link %linkopts -z max-page-size=4096 %t.o -T %t/phdrs.t -o %t.phdrs.out +# RUN: %readelf -lW %t.phdrs.out | %filecheck %s --check-prefix=PHDRS +# RUN: %link %linkopts -z max-page-size=4096 %t.o -T %t/filehdr.t -o %t.filehdr.out +# RUN: %readelf -lW %t.filehdr.out | %filecheck %s --check-prefix=FILEHDR + +# PHDRS: LOAD {{0x0*(34|40)}} {{0x0*(34|40)}} {{0x0*(34|40)}} +# PHDRS: LOAD {{0x[0-9a-f]+}} {{0x0*1000}} {{0x0*1000}} +# FILEHDR: LOAD {{0x0+}} {{0x0+}} {{0x0+}} +# FILEHDR: LOAD {{0x[0-9a-f]+}} {{0x0*1000}} {{0x0*1000}} + +#--- input.c +int data = 1; + +#--- phdrs.t +PHDRS { + headers PT_LOAD PHDRS; + data PT_LOAD; +} +SECTIONS { + . = 0x1000; + .data 0x1000 : { *(.data*) *(.sdata*) } :data +} + +#--- filehdr.t +PHDRS { + headers PT_LOAD FILEHDR; + data PT_LOAD; +} +SECTIONS { + . = 0x1000; + .data 0x1000 : { *(.data*) *(.sdata*) } :data +}