Index: ELF/LinkerScript.cpp =================================================================== --- ELF/LinkerScript.cpp +++ ELF/LinkerScript.cpp @@ -310,10 +310,36 @@ } } -template void assignOffsets(OutputSectionBase *Sec) { +// Linker script may define start and end symbols for special section types, +// like .got, .eh_frame_hdr, .eh_frame and others. Those special cases are +// handled by this function. +template +void addStartEndSymbols(OutputSectionCommand *Cmd, + OutputSectionBase *Sec) { + bool Start = true; + for (std::unique_ptr &Base : Cmd->Commands) { + if (dyn_cast(Base.get())) { + Start = false; + } else if (auto *AssignCmd = dyn_cast(Base.get())) { + if (AssignCmd->Sym) { + auto *Sym = cast>(AssignCmd->Sym); + Sym->Section = Sec; + Sym->Value = + AssignCmd->Expression(Sec->getVA() + (Start ? 0 : Sec->getSize())) - + Sec->getVA(); + } + } + } +} + +template +void assignOffsets(OutputSectionCommand *Cmd, OutputSectionBase *Sec) { auto *OutSec = dyn_cast>(Sec); if (!OutSec) { Sec->assignOffsets(); + // This section is not regular output section. However linker script may + // have defined start/end symbols for it. This case is handled below. + addStartEndSymbols(Cmd, Sec); return; } @@ -404,19 +430,19 @@ uintX_t TVA = Dot + ThreadBssOffset; TVA = alignTo(TVA, Sec->getAlignment()); Sec->setVA(TVA); - assignOffsets(Sec); + assignOffsets(Cmd, Sec); ThreadBssOffset = TVA - Dot + Sec->getSize(); continue; } if (!(Sec->getFlags() & SHF_ALLOC)) { - Sec->assignOffsets(); + assignOffsets(Cmd, Sec); continue; } Dot = alignTo(Dot, Sec->getAlignment()); Sec->setVA(Dot); - assignOffsets(Sec); + assignOffsets(Cmd, Sec); MinVA = std::min(MinVA, Dot); Dot += Sec->getSize(); } Index: test/ELF/linkerscript/linkerscript-symbols-synthetic.s =================================================================== --- test/ELF/linkerscript/linkerscript-symbols-synthetic.s +++ test/ELF/linkerscript/linkerscript-symbols-synthetic.s @@ -14,18 +14,28 @@ # RUN: begin_bar = .; \ # RUN: *(.bar) \ # RUN: end_bar = .; \ -# RUN: size_foo_2 = SIZEOF(.foo); \ -# RUN: } \ -# RUN: size_foo_3 = SIZEOF(.foo); }" > %t.script -# RUN: ld.lld -o %t1 --script %t.script %t +# RUN: size_foo_2 = SIZEOF(.foo); } \ +# RUN: size_foo_3 = SIZEOF(.foo); \ +# RUN: .eh_frame_hdr : { \ +# RUN: __eh_frame_hdr_start = .; \ +# RUN: __eh_frame_hdr_start2 = ALIGN(0x10); \ +# RUN: *(.eh_frame_hdr) \ +# RUN: __eh_frame_hdr_end = .; \ +# RUN: __eh_frame_hdr_end2 = ALIGN(0x10); } \ +# RUN: }" > %t.script +# RUN: ld.lld -o %t1 --eh-frame-hdr --script %t.script %t # RUN: llvm-objdump -t %t1 | FileCheck --check-prefix=SIMPLE %s -# SIMPLE: 0000000000000120 .foo 00000000 begin_foo -# SIMPLE-NEXT: 0000000000000128 .foo 00000000 end_foo +# SIMPLE: 0000000000000158 .foo 00000000 begin_foo +# SIMPLE-NEXT: 0000000000000160 .foo 00000000 end_foo # SIMPLE-NEXT: 0000000000000008 .foo 00000000 size_foo_1 # SIMPLE-NEXT: 0000000000001000 .foo 00000000 begin_bar # SIMPLE-NEXT: 0000000000001004 .foo 00000000 end_bar -# SIMPLE-NEXT: 0000000000000ee4 .foo 00000000 size_foo_2 -# SIMPLE-NEXT: 0000000000000ee4 *ABS* 00000000 size_foo_3 +# SIMPLE-NEXT: 0000000000000eac .foo 00000000 size_foo_2 +# SIMPLE-NEXT: 0000000000000eac *ABS* 00000000 size_foo_3 +# SIMPLE-NEXT: 0000000000001004 .eh_frame_hdr 00000000 __eh_frame_hdr_start +# SIMPLE-NEXT: 0000000000001010 .eh_frame_hdr 00000000 __eh_frame_hdr_start2 +# SIMPLE-NEXT: 0000000000001018 .eh_frame_hdr 00000000 __eh_frame_hdr_end +# SIMPLE-NEXT: 0000000000001020 .eh_frame_hdr 00000000 __eh_frame_hdr_end2 .global _start _start: @@ -36,3 +46,9 @@ .section .bar,"a" .long 0 + +.section .dah,"ax",@progbits + .cfi_startproc + nop + .cfi_endproc +