<NME> linkerscript-edata-etext.s
<BEF> ADDFILE
<MSG> [ELF] - Linkerscript: do not define _edata,_end,_etext if SECTIONS is used.

I faced that when tried to link FreeBSD kernel. 
It was "duplicate symbol: _edata in (internal) and (internal)" error.
_data was a shared symbol that came from hack.so. At first it was replaced with DefinedRegular by the code
disabled in this patch and later when script tried to define the same symbol - the error was shown.

In the same situation (as given in testcase) ld defines them as UND. gold defines as ABS with zero value. 
Patch just disables any operations of creating these symbols if script do layout.

Differential revision: https://reviews.llvm.org/D23206

git-svn-id: f6089bf0e6284f307027cef4f64114ee9ebb0424@277986 91177308-0d34-0410-b5e6-96231b3b80d8

<DFF> @@ -0,0 +1,20 @@
+# REQUIRES: x86
+# RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t.o
+# RUN: echo "SECTIONS { }" > %t.script
+# RUN: not ld.lld %t.o -script %t.script -o %t 2>&1 | FileCheck %s
+# CHECK: undefined symbol: _edata
+# CHECK: undefined symbol: _end
+# CHECK: undefined symbol: _etext
+
+.global _start,_end,_etext,_edata
+.text
+_start:
+ .quad _edata + 0x1
+ .quad _etext + 0x1
+ .quad _end + 0x1
+
+.data
+  .word 1
+.bss
+  .align 4
+  .space 6

{'commit': 'diff --git a/ELF/Writer.cpp b/ELF/Writer.cpp\nindex f98530b94..8ab906dfd 100644\n--- a/ELF/Writer.cpp\n+++ b/ELF/Writer.cpp\n@@ -578,6 +578,10 @@ template <class ELFT> void Writer<ELFT>::addReservedSymbols() {\n   if (!isOutputDynamic<ELFT>())\n     Symtab.addIgnored("__tls_get_addr");\n \n+  // If linker script do layout we do not need to create any standart symbols.\n+  if (ScriptConfig->HasContents)\n+    return;\n+\n   auto Define = [this](StringRef S, DefinedRegular<ELFT> *&Sym1,\n                        DefinedRegular<ELFT> *&Sym2) {\n     Sym1 = Symtab.addIgnored(S, STV_DEFAULT);\ndiff --git a/test/ELF/end.s b/test/ELF/end.s\nindex 689157f82..18c77a35f 100644\n--- a/test/ELF/end.s\n+++ b/test/ELF/end.s\n@@ -23,52 +23,6 @@\n // DEFAULT-NEXT:     Value: 0x12008\n // DEFAULT: ]\n \n-// If there is no .bss section, "_end" should point to the end of the .data section.\n-// RUN: echo "SECTIONS { \\\n-// RUN:          /DISCARD/ : { *(.bss) } }" > %t.script\n-// RUN: ld.lld %t.o --script %t.script -o %t\n-// RUN: llvm-readobj -sections -symbols %t | FileCheck %s --check-prefix=NOBSS\n-\n-// NOBSS: Sections [\n-// NOBSS:     Name: .data\n-// NOBSS-NEXT:     Type:\n-// NOBSS-NEXT:     Flags [\n-// NOBSS-NEXT:       SHF_ALLOC\n-// NOBSS-NEXT:       SHF_WRITE\n-// NOBSS-NEXT:     ]\n-// NOBSS-NEXT:     Address: 0x159\n-// NOBSS-NEXT:     Offset:\n-// NOBSS-NEXT:     Size: 2\n-// NOBSS: ]\n-// NOBSS: Symbols [\n-// NOBSS:     Name: _end\n-// NOBSS-NEXT:     Value: 0x15B\n-// NOBSS: ]\n-\n-// If the layout of the sections is changed, "_end" should point to the end of allocated address space.\n-// RUN: echo "SECTIONS { \\\n-// RUN:          .bss : { *(.bss) } \\\n-// RUN:          .data : { *(.data) } \\\n-// RUN:          .text : { *(.text) } }" > %t.script\n-// RUN: ld.lld %t.o --script %t.script -o %t\n-// RUN: llvm-readobj -sections -symbols %t | FileCheck %s --check-prefix=TEXTATEND\n-\n-// TEXTATEND: Sections [\n-// TEXTATEND:     Name: .text\n-// TEXTATEND-NEXT:     Type:\n-// TEXTATEND-NEXT:     Flags [\n-// TEXTATEND-NEXT:       SHF_ALLOC\n-// TEXTATEND-NEXT:       SHF_EXECINSTR\n-// TEXTATEND-NEXT:     ]\n-// TEXTATEND-NEXT:     Address: 0x160\n-// TEXTATEND-NEXT:     Offset:\n-// TEXTATEND-NEXT:     Size: 1\n-// TEXTATEND: ]\n-// TEXTATEND: Symbols [\n-// TEXTATEND:     Name: _end\n-// TEXTATEND-NEXT:     Value: 0x161\n-// TEXTATEND: ]\n-\n .global _start,_end\n .text\n _start:\ndiff --git a/test/ELF/linkerscript/linkerscript-edata-etext.s b/test/ELF/linkerscript/linkerscript-edata-etext.s\nnew file mode 100644\nindex 000000000..d5891ee0b\n--- /dev/null\n+++ b/test/ELF/linkerscript/linkerscript-edata-etext.s\n@@ -0,0 +1,20 @@\n+# REQUIRES: x86\n+# RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t.o\n+# RUN: echo "SECTIONS { }" > %t.script\n+# RUN: not ld.lld %t.o -script %t.script -o %t 2>&1 | FileCheck %s\n+# CHECK: undefined symbol: _edata\n+# CHECK: undefined symbol: _end\n+# CHECK: undefined symbol: _etext\n+\n+.global _start,_end,_etext,_edata\n+.text\n+_start:\n+ .quad _edata + 0x1\n+ .quad _etext + 0x1\n+ .quad _end + 0x1\n+\n+.data\n+  .word 1\n+.bss\n+  .align 4\n+  .space 6\n', 'commit_subject': '[ELF] - Linkerscript: do not define _edata,_end,_etext if SECTIONS is used.', 'repo_name': 'llvm-mirror/lld', 'committer': {'name': 'George Rimar', 'email': '2fd69a2f24b0bf277ee2823ffd2cdc788ba995f8@accesssoftek.com', 'time_sec': '1470645768', 'tz_offset': '0', 'date': {'seconds': '1470645768'}}, 'commit_tree': '9cc453c4da0af6d0b0340de693b233a9937869ef', 'commit_message': '[ELF] - Linkerscript: do not define _edata,_end,_etext if SECTIONS is used.\n\nI faced that when tried to link FreeBSD kernel. \nIt was "duplicate symbol: _edata in (internal) and (internal)" error.\n_data was a shared symbol that came from hack.so. At first it was replaced with DefinedRegular by the code\ndisabled in this patch and later when script tried to define the same symbol - the error was shown.\n\nIn the same situation (as given in testcase) ld defines them as UND. gold defines as ABS with zero value. \nPatch just disables any operations of creating these symbols if script do layout.\n\nDifferential revision: https://reviews.llvm.org/D23206\n\ngit-svn-id: f6089bf0e6284f307027cef4f64114ee9ebb0424@277986 91177308-0d34-0410-b5e6-96231b3b80d8\n', 'language_name': 'Python', 'license': 'apache-2.0', 'hunks': '@@ -0,0 +1,20 @@\n+# REQUIRES: x86\n+# RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t.o\n+# RUN: echo "SECTIONS { }" > %t.script\n+# RUN: not ld.lld %t.o -script %t.script -o %t 2>&1 | FileCheck %s\n+# CHECK: undefined symbol: _edata\n+# CHECK: undefined symbol: _end\n+# CHECK: undefined symbol: _etext\n+\n+.global _start,_end,_etext,_edata\n+.text\n+_start:\n+ .quad _edata + 0x1\n+ .quad _etext + 0x1\n+ .quad _end + 0x1\n+\n+.data\n+  .word 1\n+.bss\n+  .align 4\n+  .space 6\n', 'addition_count': 20, 'deletion_count': 0, 'src_file': '/dev/null', 'tgt_file': 'b/test/ELF/linkerscript/linkerscript-edata-etext.s', 'file_extension': '.s', 'before_file': 'ADDFILE', 'message': '[ELF] - Linkerscript: do not define _edata,_end,_etext if SECTIONS is used.\n\nI faced that when tried to link FreeBSD kernel. \nIt was "duplicate symbol: _edata in (internal) and (internal)" error.\n_data was a shared symbol that came from hack.so. At first it was replaced with DefinedRegular by the code\ndisabled in this patch and later when script tried to define the same symbol - the error was shown.\n\nIn the same situation (as given in testcase) ld defines them as UND. gold defines as ABS with zero value. \nPatch just disables any operations of creating these symbols if script do layout.\n\nDifferential revision: https://reviews.llvm.org/D23206\n\ngit-svn-id: f6089bf0e6284f307027cef4f64114ee9ebb0424@277986 91177308-0d34-0410-b5e6-96231b3b80d8\n', 'author_name': 'George Rimar', 'lang': 's'}