Index: lld/trunk/ELF/Writer.cpp =================================================================== --- lld/trunk/ELF/Writer.cpp +++ lld/trunk/ELF/Writer.cpp @@ -317,6 +317,16 @@ return BIsExec; // If we got here we know that both A and B are in the same PT_LOAD. + + // The TLS initialization block needs to be a single contiguous block in a R/W + // PT_LOAD, so stick TLS sections directly before R/W sections. The TLS NOBITS + // sections are placed here as they don't take up virtual address space in the + // PT_LOAD. + bool AIsTLS = AFlags & SHF_TLS; + bool BIsTLS = BFlags & SHF_TLS; + if (AIsTLS != BIsTLS) + return AIsTLS; + // The next requirement we have is to put nobits sections last. The // reason is that the only thing the dynamic linker will see about // them is a p_memsz that is larger than p_filesz. Seeing that it Index: lld/trunk/test/elf2/section-layout.s =================================================================== --- lld/trunk/test/elf2/section-layout.s +++ lld/trunk/test/elf2/section-layout.s @@ -9,10 +9,21 @@ .text _start: -.section h,"" -.section g,"",@nobits -.section f,"aw",@nobits -.section e,"aw" +.section t,"x",@nobits +.section s,"x" +.section r,"w",@nobits +.section q,"w" +.section p,"wx",@nobits +.section o,"wx" +.section n,"",@nobits +.section m,"" + +.section l,"awx",@nobits +.section k,"awx" +.section j,"aw",@nobits +.section i,"aw" +.section g,"awT",@nobits +.section e,"awT" .section d,"ax",@nobits .section c,"ax" .section b,"a",@nobits @@ -22,7 +33,22 @@ // CHECK: Name: b // CHECK: Name: c // CHECK: Name: d + +// TLS sections are only sorted on NOBITS. // CHECK: Name: e -// CHECK: Name: f -// CHECK: Name: h // CHECK: Name: g + +// CHECK: Name: i +// CHECK: Name: j +// CHECK: Name: k +// CHECK: Name: l + +// Non allocated sections are in input order. +// CHECK: Name: t +// CHECK: Name: s +// CHECK: Name: r +// CHECK: Name: q +// CHECK: Name: p +// CHECK: Name: o +// CHECK: Name: n +// CHECK: Name: m