From a695165ddf0bdbafeb905b377c7fd4acaa83c828 Mon Sep 17 00:00:00 2001 From: Parth Arora Date: Mon, 24 Nov 2025 19:04:58 +0530 Subject: [PATCH] Evaluate output section end symbol assignments in partial link Until now, the linker was not evaluating output section end symbol assignments in partial links. This commit fixes this behavior. Resolves #479 Signed-off-by: Parth Arora --- lib/Target/GNULDBackend.cpp | 1 + .../Inputs/1.c | 3 +++ .../Inputs/script.t | 15 +++++++++++++++ ...tialLinkWithOutputSectionEndAssignments.test | 17 +++++++++++++++++ 4 files changed, 36 insertions(+) create mode 100644 test/Common/standalone/linkerscript/PartialLinkWithOutputSectionEndAssignments/Inputs/1.c create mode 100644 test/Common/standalone/linkerscript/PartialLinkWithOutputSectionEndAssignments/Inputs/script.t create mode 100644 test/Common/standalone/linkerscript/PartialLinkWithOutputSectionEndAssignments/PartialLinkWithOutputSectionEndAssignments.test diff --git a/lib/Target/GNULDBackend.cpp b/lib/Target/GNULDBackend.cpp index 13358b2a9..a88742617 100644 --- a/lib/Target/GNULDBackend.cpp +++ b/lib/Target/GNULDBackend.cpp @@ -2682,6 +2682,7 @@ bool GNULDBackend::setOutputSectionOffset() { if (isCurAlloc && cur->size()) changeSymbolsFromAbsoluteToGlobal(*out); prev = cur; + evaluateAssignmentsAtEndOfOutputSection(*out); ++out; } return true; diff --git a/test/Common/standalone/linkerscript/PartialLinkWithOutputSectionEndAssignments/Inputs/1.c b/test/Common/standalone/linkerscript/PartialLinkWithOutputSectionEndAssignments/Inputs/1.c new file mode 100644 index 000000000..bd4742072 --- /dev/null +++ b/test/Common/standalone/linkerscript/PartialLinkWithOutputSectionEndAssignments/Inputs/1.c @@ -0,0 +1,3 @@ +extern int u1; +int foo() { return 1; } +int bar() { return u1; } diff --git a/test/Common/standalone/linkerscript/PartialLinkWithOutputSectionEndAssignments/Inputs/script.t b/test/Common/standalone/linkerscript/PartialLinkWithOutputSectionEndAssignments/Inputs/script.t new file mode 100644 index 000000000..48847607b --- /dev/null +++ b/test/Common/standalone/linkerscript/PartialLinkWithOutputSectionEndAssignments/Inputs/script.t @@ -0,0 +1,15 @@ +u1 = 0x100; +SECTIONS { + u2 = 0x300; + foo : { + *(.text.foo) + u3 = 0x500; + } + u4 = 0x700; + bar : { + u5 = 0x900; + *(.text.bar) + } + u5 = 0x1100; +} +u6 = 0x1300; diff --git a/test/Common/standalone/linkerscript/PartialLinkWithOutputSectionEndAssignments/PartialLinkWithOutputSectionEndAssignments.test b/test/Common/standalone/linkerscript/PartialLinkWithOutputSectionEndAssignments/PartialLinkWithOutputSectionEndAssignments.test new file mode 100644 index 000000000..51064903d --- /dev/null +++ b/test/Common/standalone/linkerscript/PartialLinkWithOutputSectionEndAssignments/PartialLinkWithOutputSectionEndAssignments.test @@ -0,0 +1,17 @@ +#---PartialLinkWithOutputSectionEndAssignments.test----- Executable,LS -----# +#BEGIN_COMMENT +# This test verifies that the linker properly evaluates the output section end +# symbol assignments when partial linking. +#END_COMMENT +#START_TEST +RUN: %clang %clangopts -o %t1.1.o %p/Inputs/1.c -c -ffunction-sections +RUN: %link %linkopts -o %t1.1.out %t1.1.o -T %p/Inputs/script.t -r +RUN: %readelf -s %t1.1.out | %filecheck %s +#END_TEST + +CHECK: 100 {{.*}} u1 +CHECK: 300 {{.*}} u2 +CHECK: 500 {{.*}} u3 +CHECK: 700 {{.*}} u4 +CHECK: 1100 {{.*}} u5 +CHECK: 1300 {{.*}} u6