Skip to content

[ELF] Orphan placement: remove hasInputSections condition #93761

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
16 changes: 5 additions & 11 deletions lld/ELF/Writer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -890,9 +890,7 @@ template <class ELFT> void Writer<ELFT>::setReservedSymbolSections() {
// countLeadingZeros.
static int getRankProximity(OutputSection *a, SectionCommand *b) {
auto *osd = dyn_cast<OutputDesc>(b);
return (osd && osd->osec.hasInputSections)
? llvm::countl_zero(a->sortRank ^ osd->osec.sortRank)
: -1;
return osd ? llvm::countl_zero(a->sortRank ^ osd->osec.sortRank) : -1;
}

// When placing orphan sections, we want to place them after symbol assignments
Expand Down Expand Up @@ -958,11 +956,6 @@ findOrphanPos(SmallVectorImpl<SectionCommand *>::iterator b,
if (i == e)
return e;

auto isOutputSecWithInputSections = [](SectionCommand *cmd) {
auto *osd = dyn_cast<OutputDesc>(cmd);
return osd && osd->osec.hasInputSections;
};

// Then, scan backward or forward through the script for a suitable insertion
// point. If i's rank is larger, the orphan section can be placed before i.
//
Expand All @@ -974,15 +967,15 @@ findOrphanPos(SmallVectorImpl<SectionCommand *>::iterator b,
bool mustAfter = script->hasPhdrsCommands() || !script->memoryRegions.empty();
if (cast<OutputDesc>(*i)->osec.sortRank <= sec->sortRank || mustAfter) {
for (auto j = ++i; j != e; ++j) {
if (!isOutputSecWithInputSections(*j))
if (!isa<OutputDesc>(*j))
continue;
if (getRankProximity(sec, *j) != maxP)
break;
i = j + 1;
}
} else {
for (; i != b; --i)
if (isOutputSecWithInputSections(i[-1]))
if (isa<OutputDesc>(i[-1]))
break;
}

Expand All @@ -991,7 +984,8 @@ findOrphanPos(SmallVectorImpl<SectionCommand *>::iterator b,
// This matches bfd's behavior and is convenient when the linker script fully
// specifies the start of the file, but doesn't care about the end (the non
// alloc sections for example).
if (std::find_if(i, e, isOutputSecWithInputSections) == e)
if (std::find_if(
i, e, [](SectionCommand *cmd) { return isa<OutputDesc>(cmd); }) == e)
return e;

while (i != e && shouldSkip(*i))
Expand Down
2 changes: 1 addition & 1 deletion lld/test/ELF/linkerscript/nobits-offset.s
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@

# CHECK: Name Type Address Off Size
# CHECK-NEXT: NULL 0000000000000000 000000 000000
# CHECK-NEXT: .text PROGBITS 0000000000000000 000190 000000
# CHECK-NEXT: .sec1 NOBITS 0000000000000000 001000 000001
# CHECK-NEXT: .text PROGBITS 0000000000000004 001000 000000
# CHECK-NEXT: .bss NOBITS 0000000000000400 001400 000001

# CHECK: Type Offset VirtAddr PhysAddr FileSiz MemSiz Flg Align
Expand Down
11 changes: 6 additions & 5 deletions lld/test/ELF/linkerscript/symbol-only-align.test
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,14 @@ SECTIONS {
## offset and addresses match.

# CHECK: Section Headers
# CHECK: foo PROGBITS 0000000000[[ADDR:[0-9a-f]*]] [[ADDR]]
# CHECK-NEXT: .data PROGBITS 0000000000[[ADDR]] [[ADDR]]
# CHECK: .text PROGBITS
# CHECK-NEXT: foo PROGBITS 0000000000001000 001000 000000
# CHECK-NEXT: .dynsym DYNSYM 0000000000001000 001000

# CHECK: Program Headers
# CHECK: LOAD
# CHECK-NEXT: LOAD 0x[[ADDR]] 0x0000000000[[ADDR]] 0x0000000000[[ADDR]]
# CHECK-NEXT: LOAD 0x001000 0x0000000000001000 0x0000000000001000

# CHECK: Symbol table
# CHECK: 0000000000[[ADDR]] 0 NOTYPE GLOBAL DEFAULT {{[0-9]+}} __start_foo
# CHECK: 0000000000[[ADDR]] 0 NOTYPE GLOBAL DEFAULT {{[0-9]+}} __end_foo
# CHECK: 0000000000001000 0 NOTYPE GLOBAL DEFAULT [[#]] __start_foo
# CHECK: 0000000000001000 0 NOTYPE GLOBAL DEFAULT [[#]] __end_foo
2 changes: 1 addition & 1 deletion lld/test/ELF/linkerscript/tls-nobits-offset.s
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@

# CHECK: Name Type Address Off Size
# CHECK-NEXT: NULL 0000000000000000 000000 000000
# CHECK-NEXT: .text PROGBITS 0000000000000000 000190 000000
# CHECK-NEXT: .sec1 PROGBITS 0000000000000000 001000 000001
# CHECK-NEXT: .text PROGBITS 0000000000000004 001004 000000
# CHECK-NEXT: .tbss NOBITS 0000000000000400 001400 000001

# CHECK: Type Offset VirtAddr PhysAddr FileSiz MemSiz Flg Align
Expand Down
Loading