Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions lld/ELF/LinkerScript.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1037,10 +1037,16 @@ void LinkerScript::addOrphanSections() {
if (ctx.arg.relocatable && (isec->flags & SHF_LINK_ORDER))
continue;

if (auto *sec = dyn_cast<InputSection>(isec))
if (InputSectionBase *rel = sec->getRelocatedSection())
if (auto *relIS = dyn_cast_or_null<InputSectionBase>(rel->parent))
if (auto *sec = dyn_cast<InputSection>(isec)) {
if (InputSectionBase *relocated = sec->getRelocatedSection()) {
// Ensure creation of OutputSection for relocated section before
// relocation section
if (auto *relIS = dyn_cast_or_null<InputSectionBase>(relocated))
add(relIS);
if (auto *relIS = dyn_cast_or_null<InputSectionBase>(relocated->parent))
add(relIS);
}
}
add(isec);
if (ctx.arg.relocatable)
for (InputSectionBase *depSec : isec->dependentSections)
Expand Down
37 changes: 37 additions & 0 deletions lld/test/ELF/linkerscript/orphan-sections-init.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# REQUIRES: x86
# RUN: rm -rf %t && mkdir -p %t
# RUN: split-file %s %t && cd %t

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add a comment to explain the test. Without context the steps look strange. LLD uses an additional comment character to distinguish comments from lit commands, so comment lines would start ##

For example:

Test that lld's orphan section placement can handle a relocatable link where the relocation section is seen before the relocated section. To create a test case we need to first need to create a relocatable object with
the relocations before the relocated section. Then we rename these to make these orphans.

I tried to see if I could create the test case without llvm-objcopy but it seemed like I needed the linker script to have .rela.text as the pattern to get it to place first.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The llvm-objcopy is unnecessary. Apply https://github.com/MaskRay/llvm-project/tree/lld-156354 to get the code/comment/test changes

## Test that lld's orphan section placement can handle a relocatable link where
## the relocation section is seen before the relocated section.

## Create a relocatable object with the relocations before the relocated section
# RUN: llvm-mc -filetype=obj -triple=x86_64 foo.s -o foo.o
# RUN: ld.lld -r foo.o -T script.ld -o foo_mc.o

## Rename the sections to make them orphans
# RUN: llvm-objcopy \
# RUN: --rename-section .text=.com.text \
# RUN: --rename-section .rela.text=.rela.com.text \
# RUN: foo_mc.o foo_mc.o

# RUN: ld.lld -r foo_mc.o -T script.ld -o foo_mc_after.o
# RUN: llvm-readelf -S foo_mc_after.o | FileCheck %s
# CHECK: .com.text PROGBITS 0000000000000000 000040 000007 00 AX 0 0 16
# CHECK-NEXT: .rela.com.text RELA 0000000000000000 000048 000018 18 I 4 1 8

#--- foo.s
.text
.globl foo
.p2align 4
.type foo,@function
foo:
mov $bar, %rax

#--- script.ld
SECTIONS
{
.rela.text 0 : { *(.rela.text) }
.text 0 : { *(.text) }
}