From c6cf1f1f02c58d15b8fe8ce9c27d9ac6b080500b Mon Sep 17 00:00:00 2001 From: George Rimar Date: Tue, 7 Feb 2017 17:51:35 +0000 Subject: [PATCH] [ELF] - Assign proper values for DefinedSynthetic symbols attached to non-allocatable sections. DefinedSynthetic symbols are attached to sections, for the case when such symbol was attached to non-allocated section, we calculated its value incorrectly. We subtracted Body->Section->Addr, but non-allocatable sections should have zero VA in output and therefore result value was wrong. And at the same time we have Body->Section->Addr != 0 for them internally because use it for calculation of section size. Patch fixes calculation of such symbols values. Differential revision: https://reviews.llvm.org/D29653 llvm-svn: 294322 --- lld/ELF/LinkerScript.cpp | 8 ++++++-- lld/test/ELF/linkerscript/symbols-and-orphans.s | 16 ++++++++++++++++ 2 files changed, 22 insertions(+), 2 deletions(-) create mode 100644 lld/test/ELF/linkerscript/symbols-and-orphans.s diff --git a/lld/ELF/LinkerScript.cpp b/lld/ELF/LinkerScript.cpp index 7ca813cee0a1..26e814b1b066 100644 --- a/lld/ELF/LinkerScript.cpp +++ b/lld/ELF/LinkerScript.cpp @@ -101,8 +101,12 @@ static void assignSymbol(SymbolAssignment *Cmd, typename ELFT::uint Dot = 0) { if (auto *Body = dyn_cast(Cmd->Sym)) { Body->Section = Cmd->Expression.Section(); - if (Body->Section) - Body->Value = Cmd->Expression(Dot) - Body->Section->Addr; + if (Body->Section) { + uint64_t VA = 0; + if (Body->Section->Flags & SHF_ALLOC) + VA = Body->Section->Addr; + Body->Value = Cmd->Expression(Dot) - VA; + } return; } diff --git a/lld/test/ELF/linkerscript/symbols-and-orphans.s b/lld/test/ELF/linkerscript/symbols-and-orphans.s new file mode 100644 index 000000000000..4184487a7817 --- /dev/null +++ b/lld/test/ELF/linkerscript/symbols-and-orphans.s @@ -0,0 +1,16 @@ +# REQUIRES: x86 +# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t + +# RUN: echo "SECTIONS { . = SIZEOF_HEADERS; \ +# RUN: .text : { *(.text) } \ +# RUN: .nonalloc : { *(.nonalloc) } \ +# RUN: Sym = .; \ +# RUN: }" > %t.script +# RUN: ld.lld -o %t2 --script %t.script %t +# RUN: llvm-objdump -section-headers -t %t2 | FileCheck %s + +# CHECK: SYMBOL TABLE: +# CHECK: 00000000000000f0 .nonalloc 00000000 Sym + +.section .nonalloc,"" + .quad 0