Skip to content

Commit 448397e

Browse files
committed
Fix only TBSS sections causing empty segment
TBSS sections are special, they do not consume any virtual memory size at load time, they incur a cost only when setting up size of the initial TLS image by either libc or libpthread. Creating a segment when a TBSS section is encountered causes linker to create an empty segment. Resolves #353 Change-Id: I0ef94d6ec9bf0bfdc60b29795effe9fc59fabaf7 Signed-off-by: Shankar Easwaran <[email protected]>
1 parent 6c91513 commit 448397e

File tree

5 files changed

+57
-8
lines changed

5 files changed

+57
-8
lines changed

lib/Target/CreateProgramHeaders.hpp

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -369,15 +369,16 @@ bool GNULDBackend::createProgramHdrs() {
369369
if (config().options().isOMagic())
370370
cur_flag = (cur_flag & ~llvm::ELF::PF_W);
371371

372-
// getSegmentFlag returns 0 if the section is not allocatable.
373-
if ((cur_flag != prev_flag) && (isCurAlloc))
374-
createPT_LOAD = true;
372+
if (!cur->isTBSS()) {
373+
if ((cur_flag != prev_flag) && (isCurAlloc))
374+
createPT_LOAD = true;
375375

376-
if (linkerScriptHasMemoryCommand && (cur_mem_region != prev_mem_region))
377-
createPT_LOAD = true;
376+
if (linkerScriptHasMemoryCommand && (cur_mem_region != prev_mem_region))
377+
createPT_LOAD = true;
378378

379-
if (linkerScriptHasMemoryCommand && (cur_mem_region != prev_mem_region))
380-
createPT_LOAD = true;
379+
if (linkerScriptHasMemoryCommand && (cur_mem_region != prev_mem_region))
380+
createPT_LOAD = true;
381+
}
381382

382383
// If the current section is alloc section and if the previous section is
383384
// NOBITS and current is PROGBITS, we need to create a new segment.
@@ -408,7 +409,7 @@ bool GNULDBackend::createProgramHdrs() {
408409

409410
// If Program headers are not specified and the vma difference is big
410411
// lets create a PT_LOAD to adjust the offset.
411-
if (std::abs(vmaoffset) > (int64_t)segAlign)
412+
if ((std::abs(vmaoffset) > (int64_t)segAlign) && (!cur->isTBSS()))
412413
createPT_LOAD = true;
413414
}
414415
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
MEMORY
2+
{
3+
RAM (rw) : ORIGIN = 10000, LENGTH = 5000
4+
}
5+
6+
SECTIONS {
7+
.foo : { *(.text.foo) } > RAM
8+
.empty : {} > RAM
9+
tbss : { *(.tbss*) } > RAM
10+
.empty : {} > RAM
11+
.main : { *(.text.main) } > RAM
12+
output_data : { BYTE(0x0) *(.data*) } > RAM
13+
/DISCARD/ : { *(.riscv.attributes) }
14+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
MEMORY
2+
{
3+
RAM (rw) : ORIGIN = 10000, LENGTH = 5000
4+
ROM (rw) : ORIGIN = 20000, LENGTH = 5000
5+
}
6+
7+
SECTIONS {
8+
.foo : { *(.text.foo) } > RAM
9+
.empty : {} > RAM
10+
tdata : { *(.tdata*) } > ROM
11+
tbss : { *(.tbss*) } > ROM
12+
.empty : {} > ROM
13+
.main : { *(.text.main) } > ROM
14+
output_data : { BYTE(0x0) *(.data*) } > ROM
15+
/DISCARD/ : { *(.riscv.attributes) }
16+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
__thread int b = 0;
2+
int data = 100;
3+
int foo() { return 0; }
4+
int main() { return 0; }
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
UNSUPPORTED: ndk-build
2+
#---OnlyTBSS.test--------------------------- Executable -----------------#
3+
#BEGIN_COMMENT
4+
# This tests that using TBSS only does not create an empty segment
5+
#END_COMMENT
6+
#START_TEST
7+
RUN: %clang %clangopts -c %p/Inputs/tls.c -o %t1.1.o -fdata-sections -ftls-model=local-exec -fPIE
8+
RUN: %link %linkopts %t1.1.o -T %p/Inputs/script.t -o %t.out --defsym __aeabi_read_tp=0
9+
RUN: %readelf -l -W %t.out | %filecheck %s -check-prefix=EMPTYLOAD
10+
RUN: %link %linkopts %t1.1.o -T %p/Inputs/scripttbssnewregion.t -o %t.out --defsym __aeabi_read_tp=0
11+
RUN: %readelf -l -W %t.out | %filecheck %s -check-prefix=EMPTYLOAD
12+
#END_TEST
13+
14+
#EMPTYLOAD-NOT: LOAD {{.*}} {{.*}} {{.*}} 0x{{[0]+}} 0x{{[0]+}} {{.*}}

0 commit comments

Comments
 (0)