Index: ELF/LinkerScript.h =================================================================== --- ELF/LinkerScript.h +++ ELF/LinkerScript.h @@ -55,6 +55,8 @@ template bool shouldKeep(InputSectionBase *S); int compareSections(StringRef A, StringRef B); + bool Exist = false; + private: template SectionRule *find(InputSectionBase *S); Index: ELF/LinkerScript.cpp =================================================================== --- ELF/LinkerScript.cpp +++ ELF/LinkerScript.cpp @@ -481,6 +481,7 @@ void LinkerScript::read(MemoryBufferRef MB) { StringRef Path = MB.getBufferIdentifier(); ScriptParser(&Alloc, MB.getBuffer(), isUnderSysroot(Path)).run(); + Exist = true; } template StringRef LinkerScript::getOutputSection(InputSectionBase *); Index: ELF/Writer.cpp =================================================================== --- ELF/Writer.cpp +++ ELF/Writer.cpp @@ -71,6 +71,10 @@ void createPhdrs(); void assignAddresses(); void assignAddressesRelocatable(); + void assignAddressesScript(); + + void assignPhdrs(); + void fixAbsoluteSymbols(); bool openFile(); void writeHeader(); @@ -210,12 +214,17 @@ addReservedSymbols(); if (!createSections()) return; - if (!Config->Relocatable) { + + if (!Config->Relocatable) createPhdrs(); - assignAddresses(); - } else { + + if (Script->Exist) + assignAddressesScript(); + else if (Config->Relocatable) assignAddressesRelocatable(); - } + else + assignAddresses(); + fixAbsoluteSymbols(); if (!openFile()) return; @@ -1381,6 +1390,51 @@ // point to the end of the data segment. ElfSym::End.st_value = VA; + assignPhdrs(); +} + +template void Writer::assignAddressesScript() { + Out::ElfHeader->setSize(sizeof(Elf_Ehdr)); + size_t PhdrSize = sizeof(Elf_Phdr) * Phdrs.size(); + Out::ProgramHeaders->setSize(PhdrSize); + + uintX_t ThreadBssOffset = 0; + uintX_t VA = 0; + uintX_t FileOff = 0; + + for (OutputSectionBase *Sec : OutputSections) { + uintX_t Align = Sec->getAlign(); + if (Sec->getType() != SHT_NOBITS) + FileOff = alignTo(FileOff, Align); + Sec->setFileOffset(FileOff); + if (Sec->getType() != SHT_NOBITS) + FileOff += Sec->getSize(); + + // We only assign VAs to allocated sections. + if (needsPtLoad(Sec)) { + VA = alignTo(VA, Align); + Sec->setVA(VA); + VA += Sec->getSize(); + } else if (Sec->getFlags() & SHF_TLS && Sec->getType() == SHT_NOBITS) { + uintX_t TVA = VA + ThreadBssOffset; + TVA = alignTo(TVA, Align); + Sec->setVA(TVA); + ThreadBssOffset = TVA - VA + Sec->getSize(); + } + } + + // Add space for section headers. + SectionHeaderOff = alignTo(FileOff, sizeof(uintX_t)); + FileSize = SectionHeaderOff + getNumSections() * sizeof(Elf_Shdr); + + // Update "_end" and "end" symbols so that they + // point to the end of the data segment. + ElfSym::End.st_value = VA; + + assignPhdrs(); +} + +template void Writer::assignPhdrs() { for (Phdr &PHdr : Phdrs) { Elf_Phdr &H = PHdr.H; if (PHdr.First) { Index: test/ELF/end.s =================================================================== --- test/ELF/end.s +++ test/ELF/end.s @@ -36,13 +36,13 @@ // NOBSS-NEXT: SHF_ALLOC // NOBSS-NEXT: SHF_WRITE // NOBSS-NEXT: ] -// NOBSS-NEXT: Address: 0x12000 +// NOBSS-NEXT: Address: 0x159 // NOBSS-NEXT: Offset: // NOBSS-NEXT: Size: 2 // NOBSS: ] // NOBSS: Symbols [ // NOBSS: Name: _end -// NOBSS-NEXT: Value: 0x12002 +// NOBSS-NEXT: Value: 0x15B // NOBSS: ] // If the layout of the sections is changed, "_end" should point to the end of allocated address space. @@ -60,13 +60,13 @@ // TEXTATEND-NEXT: SHF_ALLOC // TEXTATEND-NEXT: SHF_EXECINSTR // TEXTATEND-NEXT: ] -// TEXTATEND-NEXT: Address: 0x12000 +// TEXTATEND-NEXT: Address: 0x160 // TEXTATEND-NEXT: Offset: // TEXTATEND-NEXT: Size: 1 // TEXTATEND: ] // TEXTATEND: Symbols [ // TEXTATEND: Name: _end -// TEXTATEND-NEXT: Value: 0x12001 +// TEXTATEND-NEXT: Value: 0x161 // TEXTATEND: ] .global _start,_end Index: test/ELF/linkerscript-sections-keep.s =================================================================== --- test/ELF/linkerscript-sections-keep.s +++ test/ELF/linkerscript-sections-keep.s @@ -12,8 +12,8 @@ # SECGC: Sections: # SECGC-NEXT: Idx Name Size Address Type # SECGC-NEXT: 0 00000000 0000000000000000 -# SECGC-NEXT: 1 .text 00000007 0000000000011000 TEXT DATA -# SECGC-NEXT: 2 .temp 00000004 0000000000012000 DATA +# SECGC-NEXT: 1 .text 00000007 0000000000000158 TEXT DATA +# SECGC-NEXT: 2 .temp 00000004 000000000000015f DATA ## Now apply KEEP command to preserve the section. # RUN: echo "SECTIONS { \ @@ -26,9 +26,9 @@ # SECNOGC: Sections: # SECNOGC-NEXT: Idx Name Size Address Type # SECNOGC-NEXT: 0 00000000 0000000000000000 -# SECNOGC-NEXT: 1 .text 00000007 0000000000011000 TEXT DATA -# SECNOGC-NEXT: 2 .keep 00000004 0000000000012000 DATA -# SECNOGC-NEXT: 3 .temp 00000004 0000000000012004 DATA +# SECNOGC-NEXT: 1 .text 00000007 0000000000000158 TEXT DATA +# SECNOGC-NEXT: 2 .keep 00000004 000000000000015f DATA +# SECNOGC-NEXT: 3 .temp 00000004 0000000000000163 DATA ## A section name matches two entries in the SECTIONS directive. The ## first one doesn't have KEEP, the second one does. If section that have @@ -42,9 +42,9 @@ # KEEP-AT-FIRST: Sections: # KEEP-AT-FIRST-NEXT: Idx Name Size Address Type # KEEP-AT-FIRST-NEXT: 0 00000000 0000000000000000 -# KEEP-AT-FIRST-NEXT: 1 .keep 00000004 0000000000010120 DATA -# KEEP-AT-FIRST-NEXT: 2 .temp 00000004 0000000000010124 DATA -# KEEP-AT-FIRST-NEXT: 3 .text 00000007 0000000000011000 TEXT DATA +# KEEP-AT-FIRST-NEXT: 1 .keep 00000004 0000000000000120 DATA +# KEEP-AT-FIRST-NEXT: 2 .temp 00000004 0000000000000124 DATA +# KEEP-AT-FIRST-NEXT: 3 .text 00000007 0000000000000128 TEXT DATA # KEEP-AT-FIRST-NEXT: 4 .symtab 00000060 0000000000000000 # KEEP-AT-FIRST-NEXT: 5 .shstrtab 0000002d 0000000000000000 # KEEP-AT-FIRST-NEXT: 6 .strtab 00000012 0000000000000000 @@ -63,8 +63,8 @@ # KEEP-AT-SECOND: Sections: # KEEP-AT-SECOND-NEXT: Idx Name Size Address Type # KEEP-AT-SECOND-NEXT: 0 00000000 0000000000000000 -# KEEP-AT-SECOND-NEXT: 1 .temp 00000004 0000000000010120 DATA -# KEEP-AT-SECOND-NEXT: 2 .text 00000007 0000000000011000 TEXT DATA +# KEEP-AT-SECOND-NEXT: 1 .temp 00000004 0000000000000120 DATA +# KEEP-AT-SECOND-NEXT: 2 .text 00000007 0000000000000124 TEXT DATA # KEEP-AT-SECOND-NEXT: 3 .symtab 00000048 0000000000000000 # KEEP-AT-SECOND-NEXT: 4 .shstrtab 00000027 0000000000000000 # KEEP-AT-SECOND-NEXT: 5 .strtab 0000000d 0000000000000000 Index: test/ELF/linkerscript-va.s =================================================================== --- test/ELF/linkerscript-va.s +++ test/ELF/linkerscript-va.s @@ -0,0 +1,24 @@ +# REQUIRES: x86 +# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t + +# RUN: echo "" > %t.script +# RUN: ld.lld -o %t1 --script %t.script %t +# RUN: llvm-objdump -section-headers %t1 | FileCheck %s +# CHECK: Sections: +# CHECK-NEXT: Idx Name Size Address Type +# CHECK-NEXT: 0 00000000 0000000000000000 +# CHECK-NEXT: 1 .foo 00000004 0000000000000120 DATA +# CHECK-NEXT: 2 .boo 00000004 0000000000000124 DATA +# CHECK-NEXT: 3 .text 00000001 0000000000000128 TEXT DATA + +.global _start +_start: + nop + +.section .foo, "a" +foo: + .long 0 + +.section .boo, "a" +boo: + .long 0 Index: test/ELF/wildcards.s =================================================================== --- test/ELF/wildcards.s +++ test/ELF/wildcards.s @@ -10,10 +10,10 @@ # SEC-DEFAULT: Sections: # SEC-DEFAULT-NEXT: Idx Name Size Address Type # SEC-DEFAULT-NEXT: 0 00000000 0000000000000000 -# SEC-DEFAULT-NEXT: 1 .text 00000008 0000000000011000 TEXT DATA -# SEC-DEFAULT-NEXT: 2 .abcd 00000004 0000000000011008 TEXT DATA -# SEC-DEFAULT-NEXT: 3 .ad 00000004 000000000001100c TEXT DATA -# SEC-DEFAULT-NEXT: 4 .ag 00000004 0000000000011010 TEXT DATA +# SEC-DEFAULT-NEXT: 1 .text 00000008 0000000000000120 TEXT DATA +# SEC-DEFAULT-NEXT: 2 .abcd 00000004 0000000000000128 TEXT DATA +# SEC-DEFAULT-NEXT: 3 .ad 00000004 000000000000012c TEXT DATA +# SEC-DEFAULT-NEXT: 4 .ag 00000004 0000000000000130 TEXT DATA # SEC-DEFAULT-NEXT: 5 .symtab 00000030 0000000000000000 # SEC-DEFAULT-NEXT: 6 .shstrtab 0000002f 0000000000000000 # SEC-DEFAULT-NEXT: 7 .strtab 00000008 0000000000000000 @@ -34,9 +34,9 @@ # SEC-ALL: Sections: # SEC-ALL-NEXT: Idx Name Size Address Type # SEC-ALL-NEXT: 0 00000000 0000000000000000 -# SEC-ALL-NEXT: 1 .text 0000000c 0000000000011000 TEXT DATA -# SEC-ALL-NEXT: 2 .ad 00000004 000000000001100c TEXT DATA -# SEC-ALL-NEXT: 3 .ag 00000004 0000000000011010 TEXT DATA +# SEC-ALL-NEXT: 1 .text 0000000c 0000000000000120 TEXT DATA +# SEC-ALL-NEXT: 2 .ad 00000004 000000000000012c TEXT DATA +# SEC-ALL-NEXT: 3 .ag 00000004 0000000000000130 TEXT DATA # SEC-ALL-NEXT: 4 .symtab 00000030 0000000000000000 # SEC-ALL-NEXT: 5 .shstrtab 00000029 0000000000000000 # SEC-ALL-NEXT: 6 .strtab 00000008 0000000000000000 @@ -50,7 +50,7 @@ # SEC-NO: Sections: # SEC-NO-NEXT: Idx Name Size Address Type # SEC-NO-NEXT: 0 00000000 0000000000000000 -# SEC-NO-NEXT: 1 .text 00000014 0000000000011000 TEXT DATA +# SEC-NO-NEXT: 1 .text 00000014 0000000000000120 TEXT DATA # SEC-NO-NEXT: 2 .symtab 00000030 0000000000000000 # SEC-NO-NEXT: 3 .shstrtab 00000021 0000000000000000 # SEC-NO-NEXT: 4 .strtab 00000008 0000000000000000