[llvm-branch-commits] [lld] 704e0ba - [lld][WebAssembly] Honor --allow-undefined for data symbols too

2020-05-01 Thread Sam Clegg via llvm-branch-commits

Author: Sam Clegg
Date: 2020-05-01T10:08:58-07:00
New Revision: 704e0ba53cb2d849fef08d1850175de8efd814c7

URL: 
https://github.com/llvm/llvm-project/commit/704e0ba53cb2d849fef08d1850175de8efd814c7
DIFF: 
https://github.com/llvm/llvm-project/commit/704e0ba53cb2d849fef08d1850175de8efd814c7.diff

LOG: [lld][WebAssembly] Honor --allow-undefined for data symbols too

This was originally the way this worked before before
https://reviews.llvm.org/D60882.

In retrospect it seems inconsistent that `--allow-undefined` doesn't
work for all symbols.  See:
https://groups.google.com/g/emscripten-discuss/c/HSRgQiIq1gI/m/Kt9oFWHiAwAJ

I'm also planning a followup change which implement the full
`--unresolved-symbols=..` flags supported by ELF linkers (both ld and
ld.lld) since it seems more standard.

Added: 


Modified: 
lld/test/wasm/undefined-data.ll
lld/wasm/Relocations.cpp

Removed: 




diff  --git a/lld/test/wasm/undefined-data.ll b/lld/test/wasm/undefined-data.ll
index e19086c2e0e0..59f7407bd610 100644
--- a/lld/test/wasm/undefined-data.ll
+++ b/lld/test/wasm/undefined-data.ll
@@ -1,6 +1,6 @@
 ; RUN: llc -filetype=obj %s -o %t.o
 ; RUN: not wasm-ld -o %t.wasm %t.o 2>&1 | FileCheck %s -check-prefix=UNDEF
-; RUN: not wasm-ld --allow-undefined -o %t.wasm %t.o 2>&1 | FileCheck %s 
-check-prefix=UNDEF
+; RUN: wasm-ld --allow-undefined -o %t.wasm %t.o
 ; RUN: not wasm-ld --shared -o %t.wasm %t.o 2>&1 | FileCheck %s 
-check-prefix=SHARED
 
 target triple = "wasm32-unknown-unknown"

diff  --git a/lld/wasm/Relocations.cpp b/lld/wasm/Relocations.cpp
index 9475ff1c312e..e49acec244c1 100644
--- a/lld/wasm/Relocations.cpp
+++ b/lld/wasm/Relocations.cpp
@@ -21,13 +21,6 @@ static bool requiresGOTAccess(const Symbol *sym) {
 }
 
 static bool allowUndefined(const Symbol* sym) {
-  // Historically --allow-undefined doesn't work for data symbols since we 
don't
-  // have any way to represent these as imports in the final binary.  The idea
-  // behind allowing undefined symbols is to allow importing these symbols from
-  // the embedder and we can't do this for data symbols (at least not without
-  // compiling with -fPIC)
-  if (isa(sym))
-return false;
   // Undefined functions with explicit import name are allowed to be undefined
   // at link time.
   if (auto *F = dyn_cast(sym))



___
llvm-branch-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [llvm] d4d4c6b - [llvm-objcopy] -O binary: skip empty sections

2020-05-01 Thread Fangrui Song via llvm-branch-commits

Author: Fangrui Song
Date: 2020-05-01T20:42:53-07:00
New Revision: d4d4c6bf834142326301a743d2939e868d9f0f0f

URL: 
https://github.com/llvm/llvm-project/commit/d4d4c6bf834142326301a743d2939e868d9f0f0f
DIFF: 
https://github.com/llvm/llvm-project/commit/d4d4c6bf834142326301a743d2939e868d9f0f0f.diff

LOG: [llvm-objcopy] -O binary: skip empty sections

After SHF_ALLOC sections are ordered by LMA:

* If initial sections are empty, GNU objcopy skips their contents while we
  emit leading zeros. (binary-paddr.test %t4)
* If trailing sections are empty, GNU objcopy skips their contents while we
  emit trailing zeros. (binary-paddr.test %t5)

This patch matches GNU objcopy's behavior. Linkers don't keep p_memsz
PT_LOAD segments. Such empty sections would not have a containing
PT_LOAD and `Section::ParentSegment` might be null if linkers fail to
optimize the file offsets (lld D79254).

In particular, without D79254, the arm Linux kernel's multi_v5_defconfig
depends on this behavior: in `vmlinux`, an empty .text_itcm is mapped at
a very high address (0xfffe) but the kernel does not expect
`objcopy -O binary` to create a very large `arch/arm/boot/Image`
(0xfffe-0xc000 ~= 1GiB). See https://bugs.llvm.org/show_bug.cgi?id=45632

Reviewed By: jhenderson

Differential Revision: https://reviews.llvm.org/D79229

(cherry picked from commit ec786906f5feb4dceba1b5338927079e63e78095)

Added: 


Modified: 
llvm/test/tools/llvm-objcopy/ELF/binary-paddr.test
llvm/tools/llvm-objcopy/ELF/Object.cpp

Removed: 




diff  --git a/llvm/test/tools/llvm-objcopy/ELF/binary-paddr.test 
b/llvm/test/tools/llvm-objcopy/ELF/binary-paddr.test
index 5ac692dc5f86..af961422a155 100644
--- a/llvm/test/tools/llvm-objcopy/ELF/binary-paddr.test
+++ b/llvm/test/tools/llvm-objcopy/ELF/binary-paddr.test
@@ -48,7 +48,7 @@ ProgramHeaders:
 # RUN: od -A x -t x2 %t2.out | FileCheck %s --check-prefix=CHECK2 --ignore-case
 # RUN: wc -c %t2.out | FileCheck %s --check-prefix=SIZE2
 
-## The computed LMA of .data is 0x4000. The minimum LMA of all sections is 
0x1000.
+## The computed LMA of .data is 0x4000. The minimum LMA of all non-empty 
sections is 0x1000.
 ## The content of .data will be written at 0x4000-0x1000 = 0x3000.
 # CHECK2:  00 c3c3 c3c3      
 # CHECK2-NEXT: 10        
@@ -93,7 +93,7 @@ ProgramHeaders:
 # RUN: od -A x -t x2 %t3.out | FileCheck %s --check-prefix=CHECK3 --ignore-case
 # RUN: wc -c %t3.out | FileCheck %s --check-prefix=SIZE3
 
-## The minimum LMA of all sections is 0x1000.
+## The minimum LMA of all non-empty sections is 0x1000.
 ## The content of .data will be written at 0x3000-0x1000 = 0x2000.
 # CHECK3:  00 c3c3 c3c3      
 # CHECK3-NEXT: 10        
@@ -130,3 +130,58 @@ ProgramHeaders:
 PAddr: 0x3000
 Sections:
   - Section: .data
+
+## The first section (.text) is empty. Test that we skip its LMA until the 
first
+## non-empty section, otherwise we would leave a large number of leading 
zeroes.
+# RUN: yaml2obj --docnum=4 %s -o %t4
+# RUN: llvm-objcopy -O binary %t4 %t4.out
+# RUN: od -A x -t x2 %t4.out | FileCheck %s --check-prefix=SKIPEMPTY
+
+# SKIPEMPTY:  00 3232
+# SKIPEMPTY-NEXT: 02
+
+--- !ELF
+FileHeader:
+  Class:   ELFCLASS64
+  Data:ELFDATA2LSB
+  Type:ET_EXEC
+  Machine: EM_X86_64
+Sections:
+  - Name: .text
+Type: SHT_PROGBITS
+Flags:[ SHF_ALLOC, SHF_EXECINSTR ]
+Address:  0x1000
+AddressAlign: 0x1000
+  - Name: gap
+Type: Fill
+Size: 0x1000
+  - Name: .data
+Type: SHT_PROGBITS
+Flags:[ SHF_ALLOC, SHF_WRITE ]
+Content:  "3232"
+
+## The last section (.data) is empty. Test that we stop dumping after the last
+## non-empty section, otherwise we would leave a large number of trailing 
zeroes.
+# RUN: yaml2obj --docnum=5 %s -o %t5
+# RUN: llvm-objcopy -O binary %t5 %t5.out
+# RUN: od -A x -t x2 %t5.out | FileCheck %s --check-prefix=SKIPEMPTY
+
+--- !ELF
+FileHeader:
+  Class:   ELFCLASS64
+  Data:ELFDATA2LSB
+  Type:ET_EXEC
+  Machine: EM_X86_64
+Sections:
+  - Name: .text
+Type: SHT_PROGBITS
+Flags:[ SHF_ALLOC, SHF_EXECINSTR ]
+Address:  0x1000
+AddressAlign: 0x1000
+Content:  "3232"
+  - Name: gap
+Type: Fill
+Size: 0xffd
+  - Name: .data
+Type: SHT_PROGBITS
+Flags:[ SHF_ALLOC, SHF_WRITE ]

diff  --git a/llvm/tools/llvm-objcopy/ELF/Object.cpp 
b/llvm/tools/llvm-objcopy/ELF/Object.cpp
index ad53c75663ec..98f72959ae55 100644
--- a/llvm/tools/llvm-objcopy/ELF/Object.cpp
+++ b/llvm/tools/llvm-objcopy/ELF/Object.cpp
@@ -2254,25 +2254,26 @@ Error BinaryWriter::finalize() {
   OrderedSegments.erase(End, std::end(OrderedSeg