[PATCH] D42911: [ELF] - Make defsym to work correctly with reserved symbols.

George Rimar via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Feb 5 08:15:31 PST 2018


grimar created this revision.
grimar added reviewers: ruiu, espindola.
Herald added a subscriber: emaste.

Fixes PR35744.

Bug happens because of different nature of reserved symbols.

We add them with value -1 and output section as target section:

  auto Add = [](StringRef S, int64_t Pos) {
    return addOptionalRegular(S, Out::ElfHeader, Pos, STV_DEFAULT);
  };
  ElfSym::Etext1 = Add("etext", -1);

Then out code handles -1 as special case:

  uint64_t SectionBase::getOffset(uint64_t Offset) const {
  ...
  // For output sections we treat offset -1 as the end of the section.
  return Offset == uint64_t(-1) ? OS->Size : Offset;

Though when we handle scripts and particulary expressions like
foo = etext + 1;
this might change special (-1) value and break things.

Patch fixes the issue.


https://reviews.llvm.org/D42911

Files:
  ELF/LinkerScript.cpp
  test/ELF/defsym-reserved-syms.s


Index: test/ELF/defsym-reserved-syms.s
===================================================================
--- test/ELF/defsym-reserved-syms.s
+++ test/ELF/defsym-reserved-syms.s
@@ -0,0 +1,30 @@
+# REQUIRES: x86
+# RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t.o
+# RUN: ld.lld -o %t %t.o --defsym=foo2=etext
+# RUN: llvm-readobj -t -s %t | FileCheck %s
+
+## Check 'foo2' value is equal to value of 'etext'.
+# CHECK:     Symbol {
+# CHECK:      Name: foo2
+# CHECK-NEXT:  Value: 0x[[VAL:.*]]
+# CHECK:     Symbol {
+# CHECK:      Name: etext
+# CHECK-NEXT:  Value: 0x[[VAL]]
+
+## Check 'foo2' value set correctly when using
+## reserved symbol 'etext' in expression.
+# RUN: ld.lld -o %t %t.o --defsym=foo2=etext+2
+# RUN: llvm-readobj -t -s %t | FileCheck %s --check-prefix=EXPR
+# EXPR:     Symbol {
+# EXPR:      Name: foo2
+# EXPR-NEXT:  Value: 0x201007
+# EXPR:     Symbol {
+# EXPR:      Name: etext
+# EXPR-NEXT:  Value: 0x201005
+
+.globl foo1
+ foo1 = 0x123
+
+.global _start
+_start:
+  movl $foo2, %edx
Index: ELF/LinkerScript.cpp
===================================================================
--- ELF/LinkerScript.cpp
+++ ELF/LinkerScript.cpp
@@ -74,7 +74,10 @@
   // If the alignment is trivial, we don't have to compute the full
   // value to know the offset. This allows this function to succeed in
   // cases where the output section is not yet known.
-  if (Alignment == 1)
+  // In case of output section we always want to compute the full value
+  // because use special offset value (-1) as end of section mark.
+  bool IsOutput = Sec && isa<OutputSection>(Sec);
+  if (!IsOutput && Alignment == 1)
     return Val;
   return getValue() - getSecAddr();
 }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D42911.132836.patch
Type: text/x-patch
Size: 1701 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20180205/644edaeb/attachment.bin>


More information about the llvm-commits mailing list