Skip to content

Commit aa4fb10

Browse files
parth-07quic-seaswara
authored andcommitted
Add a warning for empty segments
This commit improves linker diagnostics by adding a warning diagnostic for empty segments. Closes #116 Signed-off-by: Parth Arora <[email protected]>
1 parent b655e2c commit aa4fb10

File tree

6 files changed

+37
-0
lines changed

6 files changed

+37
-0
lines changed

include/eld/Diagnostics/DiagLayouts.inc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,3 +61,5 @@ DIAG(warning_zero_sized_fragment_for_non_zero_symbol, DiagnosticEngine::Warning,
6161
"Zero sized fragment %0 for non zero sized symbol %1 from input file %2")
6262
DIAG(error_offset_not_assigned_for_output_section, DiagnosticEngine::Error,
6363
"Requested offset for output section %0 has not yet been assigned")
64+
DIAG(warn_empty_segment, DiagnosticEngine::Warning,
65+
"Empty segment: '%0'")

include/eld/Target/GNULDBackend.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -944,6 +944,8 @@ class GNULDBackend {
944944
// --------------------------Provide target specific symbol values -------
945945
void resolveTargetDefinedSymbols();
946946

947+
bool verifySegments() const;
948+
947949
private:
948950
// Reserved segments.
949951
int32_t m_NumReservedSegments = 0;

lib/Target/GNULDBackend.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4034,6 +4034,8 @@ bool GNULDBackend::relax() {
40344034
return config().getDiagEngine()->diagnose();
40354035
}
40364036

4037+
verifySegments();
4038+
40374039
return config().getDiagEngine()->diagnose();
40384040
}
40394041

@@ -5169,3 +5171,12 @@ bool GNULDBackend::allocateHeaders() {
51695171
}
51705172
return false;
51715173
}
5174+
5175+
bool GNULDBackend::verifySegments() const {
5176+
const auto &SegTable = elfSegmentTable();
5177+
for (const auto &S : SegTable.segments()) {
5178+
if (S->empty())
5179+
config().raise(Diag::warn_empty_segment) << S->name();
5180+
}
5181+
return true;
5182+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
int foo() { return 1; }
2+
3+
int var = 3;
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
PHDRS {
2+
A PT_LOAD;
3+
B PT_LOAD;
4+
}
5+
6+
SECTIONS {
7+
.text : { *(.text*) } :B
8+
.data : { *(.data*) }
9+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#---WarnEmptySegments.test--------------------- Executable,LS------------------#
2+
#BEGIN_COMMENT
3+
# This tests checks that the linker emits a warning for empty segments.
4+
#END_COMMENT
5+
#START_TEST
6+
RUN: %clang %clangopts -o %t1.1.o -c %p/Inputs/1.c
7+
RUN: %link %linkopts -o %t1.1.out %t1.1.o -T %p/Inputs/script.t 2>&1 | %filecheck %s
8+
#END_TEST
9+
10+
CHECK: Warning: Empty segment: 'A'

0 commit comments

Comments
 (0)