Skip to content

Commit d3ac1d3

Browse files
Andreu Carminatiluismarques
authored andcommitted
[ELF] Refine warning condition for memory region assignment for non-allocatable section
The warning "ignoring memory region assignment for non-allocatable section" should be generated under the following conditions: * sections without SHF_ALLOC attribute and, * presence of input sections or data commands (ByteCommand) The goal of the change is to reduce spurious warnings that are generated for some output sections that have no input section. Reviewed By: MaskRay, peter.smith Differential Revision: https://reviews.llvm.org/D151802
1 parent 2e46e90 commit d3ac1d3

File tree

2 files changed

+65
-1
lines changed

2 files changed

+65
-1
lines changed

lld/ELF/LinkerScript.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -909,7 +909,12 @@ std::pair<MemoryRegion *, MemoryRegion *>
909909
LinkerScript::findMemoryRegion(OutputSection *sec, MemoryRegion *hint) {
910910
// Non-allocatable sections are not part of the process image.
911911
if (!(sec->flags & SHF_ALLOC)) {
912-
if (!sec->memoryRegionName.empty())
912+
bool hasInputOrByteCommand =
913+
sec->hasInputSections ||
914+
llvm::any_of(sec->commands, [](SectionCommand *comm) {
915+
return ByteCommand::classof(comm);
916+
});
917+
if (!sec->memoryRegionName.empty() && hasInputOrByteCommand)
913918
warn("ignoring memory region assignment for non-allocatable section '" +
914919
sec->name + "'");
915920
return {nullptr, nullptr};
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# REQUIRES: x86
2+
3+
# RUN: split-file %s %t
4+
# RUN: llvm-mc -filetype=obj -triple=x86_64 %t/a.s -o %t/a.o
5+
6+
## Check if the linker can generate the final file, containing all non-empty sections,
7+
## without warnings for .intvec0_out, .intvec1_out1, .intvec2_out, because their
8+
## input sections are empty. Althoung, the linker must generate a warning for the
9+
## first ".nonalloc" section and the second .dat, as expected.
10+
11+
# RUN: ld.lld %t/a.o -T %t/a.lds -o %t/a.elf 2>&1 | FileCheck %s --check-prefix=WARN --implicit-check-not=warning:
12+
13+
# WARN: warning: ignoring memory region assignment for non-allocatable section '.nonalloc'
14+
# WARN: warning: ignoring memory region assignment for non-allocatable section '.dat'
15+
16+
## The output file must include all sections.
17+
# RUN: llvm-readelf -S %t/a.elf | FileCheck %s
18+
19+
# CHECK: There are 12 section headers, starting at offset 0x2140:
20+
# CHECK: [Nr] Name Type Address Off Size ES Flg Lk Inf Al
21+
# CHECK-NEXT: [ 0] NULL 0000000000000000 000000 000000 00 0 0 0
22+
# CHECK-NEXT: [ 1] .nonalloc PROGBITS 0000000000000000 001064 001000 00 W 0 0 1
23+
# CHECK-NEXT: [ 2] .comment PROGBITS 0000000000000000 {{.*}} {{.*}} 01 MS 0 0 1
24+
# CHECK-NEXT: [ 3] .symtab SYMTAB 0000000000000000 {{.*}} {{.*}} 18 5 1 8
25+
# CHECK-NEXT: [ 4] .shstrtab STRTAB 0000000000000000 {{.*}} {{.*}} 00 0 0 1
26+
# CHECK-NEXT: [ 5] .strtab STRTAB 0000000000000000 {{.*}} {{.*}} 00 0 0 1
27+
# CHECK-NEXT: [ 6] .dat PROGBITS 0000000000000000 002137 000004 00 W 0 0 1
28+
# CHECK-NEXT: [ 7] .intvec0_out PROGBITS 0000000000000000 00213b 000000 00 W 0 0 1
29+
# CHECK-NEXT: [ 8] .intvec1_out PROGBITS 0000000000000000 00213b 000000 00 W 0 0 1
30+
# CHECK-NEXT: [ 9] .intvec2_out PROGBITS 0000000000000000 00213b 000000 00 W 0 0 1
31+
# CHECK-NEXT: [10] .intvec3_out PROGBITS 00000000803fe060 001060 000004 00 AX 0 0 1
32+
# CHECK-NEXT: [11] .text PROGBITS 00000000803fe064 001064 000000 00 AX 0 0 4
33+
34+
35+
#--- a.s
36+
.global _start
37+
.text
38+
_start:
39+
.section .nonalloc,"w"
40+
.zero 0x1000
41+
.section .intvec3,"ax",@progbits
42+
.long 1
43+
44+
#--- a.lds
45+
MEMORY {
46+
MEM1 : ORIGIN = 0x10000000, LENGTH = 1M
47+
MEM2 (rx) : ORIGIN = 0x80000000, LENGTH = 4M
48+
}
49+
50+
VEC_START = 0x803FE000;
51+
52+
SECTIONS {
53+
.nonalloc : { *(.nonalloc) } > MEM
54+
.dat : { LONG(0); } > MEM1
55+
.intvec0_out (VEC_START + 0x0000) : {KEEP (*(.intvec1)) } > MEM2
56+
.intvec1_out (VEC_START + 0x0020) : {KEEP (*(.intvec1)) } > MEM2
57+
.intvec2_out (VEC_START + 0x0040) : {KEEP (*(.intvec2)) } > MEM2
58+
.intvec3_out (VEC_START + 0x0060) : {KEEP (*(.intvec3)) } > MEM2
59+
}

0 commit comments

Comments
 (0)