Skip to content

Commit 4c3b74b

Browse files
committed
[LLD] [COFF] Order .debug_* sections at the end, to avoid leaving gaps if stripped
So far, we sort all discardable sections at the end, with only some extra logic to make sure that the .reloc section is at the start of that group of sections. But if there are other discardable sections, other than .reloc, they must also be ordered before .debug_* sections, to avoid leaving gaps if the executable is stripped. (Stripping executables doesn't remove all discardable sections, only the ones named .debug_*). Rust binaries seem to include a .rmeta section, which is marked discardable. This fixes stripping such binaries if built with dwarf debug info included. This fixes issues observed in MSYS2 in msys2/MINGW-packages#10555. Differential Revision: https://reviews.llvm.org/D120805
1 parent a5605c9 commit 4c3b74b

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

lld/COFF/Writer.cpp

+7-1
Original file line numberDiff line numberDiff line change
@@ -928,8 +928,14 @@ void Writer::createSections() {
928928
// Move DISCARDABLE (or non-memory-mapped) sections to the end of file
929929
// because the loader cannot handle holes. Stripping can remove other
930930
// discardable ones than .reloc, which is first of them (created early).
931-
if (s->header.Characteristics & IMAGE_SCN_MEM_DISCARDABLE)
931+
if (s->header.Characteristics & IMAGE_SCN_MEM_DISCARDABLE) {
932+
// Move discardable sections named .debug_ to the end, after other
933+
// discardable sections. Stripping only removes the sections named
934+
// .debug_* - thus try to avoid leaving holes after stripping.
935+
if (s->name.startswith(".debug_"))
936+
return 3;
932937
return 2;
938+
}
933939
// .rsrc should come at the end of the non-discardable sections because its
934940
// size may change by the Win32 UpdateResources() function, causing
935941
// subsequent sections to move (see https://crbug.com/827082).

lld/test/COFF/sort-debug.test

+6
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
# CHECK: Name: .text
1212
# CHECK: Name: .reloc
13+
# CHECK: Name: .rmeta
1314
# CHECK: Name: .debug_abbrev
1415
# CHECK: Name: .debug_info
1516
# CHECK: Name: .debug_line
@@ -18,6 +19,7 @@
1819

1920
# NODEBUG: Name: .text
2021
# NODEBUG: Name: .reloc
22+
# NODEBUG: Name: .rmeta
2123
# NODEBUG-NOT: Name: .debug_abbrev
2224
# NODEBUG-NOT: Name: .debug_info
2325
# NODEBUG-NOT: Name: .debug_line
@@ -183,6 +185,10 @@ sections:
183185
- VirtualAddress: 43
184186
SymbolName: .text
185187
Type: IMAGE_REL_I386_DIR32
188+
- Name: .rmeta
189+
Characteristics: [ IMAGE_SCN_CNT_INITIALIZED_DATA, IMAGE_SCN_MEM_DISCARDABLE, IMAGE_SCN_MEM_READ ]
190+
Alignment: 1
191+
SectionData: 00112233
186192
symbols:
187193
- Name: .text
188194
Value: 0

0 commit comments

Comments
 (0)