Skip to content

Commit 342040b

Browse files
committed
[lld][ELF][test] Add additional test coverage for LTO
These are all inspired by existing test coverage we have in an internal testsuite. Reviewed by: grimar, MaskRay Differential Revision: https://reviews.llvm.org/D89775
1 parent 86a480e commit 342040b

File tree

6 files changed

+145
-23
lines changed

6 files changed

+145
-23
lines changed

lld/test/ELF/lto/Inputs/common.s

Lines changed: 0 additions & 1 deletion
This file was deleted.

lld/test/ELF/lto/Inputs/resolution.s

Lines changed: 0 additions & 4 deletions
This file was deleted.

lld/test/ELF/lto/bitcode-wrapper.ll

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
; REQUIRES: x86
2+
3+
;; The LLVM bitcode format allows for an optional wrapper header. This test
4+
;; shows that LLD can handle bitcode wrapped in this way, and also that an
5+
;; invalid offset in the wrapper header is handled cleanly.
6+
7+
; RUN: rm -rf %t
8+
; RUN: split-file %s %t
9+
; RUN: llvm-as %t/ir.ll -o %t.bc
10+
11+
;; Basic case:
12+
; RUN: %python %t/wrap_bitcode.py %t.bc %t.o 0 0x14
13+
; RUN: ld.lld %t.o -o %t.elf
14+
; RUN: llvm-readelf -s %t.elf | FileCheck %s
15+
16+
;; Padding between wrapper header and body:
17+
; RUN: %python %t/wrap_bitcode.py %t.bc %t.o 0x10 0x24
18+
; RUN: ld.lld %t.o -o %t.elf
19+
; RUN: llvm-readelf -s %t.elf | FileCheck %s
20+
21+
; CHECK: _start
22+
23+
;; Invalid offset past end of file:
24+
; RUN: %python %t/wrap_bitcode.py %t.bc %t2.o 0x10 0xffffffff
25+
; RUN: not ld.lld %t2.o -o %t2.elf 2>&1 | FileCheck %s --check-prefix=ERR1 -DFILE=%t2.o
26+
27+
; ERR1: error: [[FILE]]: Invalid bitcode wrapper header
28+
29+
;; Invalid offset within file:
30+
; RUN: %python %t/wrap_bitcode.py %t.bc %t3.o 0x10 0x14
31+
; RUN: not ld.lld %t3.o -o %t3.elf 2>&1 | FileCheck %s --check-prefix=ERR2 -DFILE=%t3.o
32+
33+
; ERR2: error: [[FILE]]: file doesn't start with bitcode header
34+
35+
;--- ir.ll
36+
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
37+
target triple = "x86_64-unknown-linux-gnu"
38+
39+
@_start = global i32 0
40+
41+
;--- wrap_bitcode.py
42+
## Arguments are: input file, output file, padding size, offset value.
43+
import struct
44+
import sys
45+
46+
with open(sys.argv[1], 'rb') as input:
47+
bitcode = input.read()
48+
49+
padding = int(sys.argv[3], 16) * b'\0'
50+
offset = int(sys.argv[4], 16)
51+
header = struct.pack('<IIIII', 0x0B17C0DE, 0, offset, len(bitcode), 0)
52+
with open(sys.argv[2], 'wb') as output:
53+
output.write(header)
54+
output.write(padding)
55+
output.write(bitcode)

lld/test/ELF/lto/common.ll

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,26 @@
11
; REQUIRES: x86
2-
; RUN: llvm-as %s -o %t1.o
3-
; RUN: llvm-mc -triple=x86_64-pc-linux %p/Inputs/common.s -o %t2.o -filetype=obj
4-
; RUN: ld.lld %t1.o %t2.o -o %t.so -shared
5-
; RUN: llvm-readobj -S --symbols %t.so | FileCheck %s
2+
3+
;; Show that common symbols defined in both native objects and bitcode files are
4+
;; properly resolved.
5+
6+
; RUN: rm -rf %t.dir
7+
; RUN: split-file %s %t.dir
8+
; RUN: cd %t.dir
9+
10+
;; Case 1: bitcode file has smaller symbol but larger alignment.
11+
; RUN: llvm-as smaller-sym.ll -o smaller-sym.bc
12+
; RUN: llc -filetype=obj larger-sym.ll -o larger-sym.o
13+
; RUN: ld.lld smaller-sym.bc larger-sym.o -o 1.so -shared
14+
; RUN: llvm-readobj -S --symbols 1.so | FileCheck %s -DALIGN=8
15+
16+
;; Case 2: bitcode file has larger symbol but smaller alignment.
17+
; RUN: llvm-as larger-sym.ll -o larger-sym.bc
18+
; RUN: llc -filetype=obj smaller-sym.ll -o smaller-sym.o
19+
; RUN: ld.lld smaller-sym.o larger-sym.bc -o 2.so -shared
20+
;; FIXME: This alignment should be 8, but LLD is ignoring the alignment of a
21+
;; symbol in a native object file when the larger symbol is in a bitcode file.
22+
;; See https://bugs.llvm.org/show_bug.cgi?id=47819.
23+
; RUN: llvm-readobj -S --symbols 2.so | FileCheck %s -DALIGN=4
624

725
; CHECK: Name: .bss
826
; CHECK-NEXT: Type: SHT_NOBITS
@@ -12,20 +30,27 @@
1230
; CHECK-NEXT: ]
1331
; CHECK-NEXT: Address:
1432
; CHECK-NEXT: Offset:
15-
; CHECK-NEXT: Size: 8
33+
; CHECK-NEXT: Size: 2
1634
; CHECK-NEXT: Link: 0
1735
; CHECK-NEXT: Info: 0
18-
; CHECK-NEXT: AddressAlignment: 8
36+
; CHECK-NEXT: AddressAlignment: [[ALIGN]]
1937

2038
; CHECK: Name: a
2139
; CHECK-NEXT: Value:
22-
; CHECK-NEXT: Size: 8
40+
; CHECK-NEXT: Size: 2
2341
; CHECK-NEXT: Binding: Global
2442
; CHECK-NEXT: Type: Object
2543
; CHECK-NEXT: Other: 0
2644
; CHECK-NEXT: Section: .bss
2745

46+
;--- smaller-sym.ll
47+
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
48+
target triple = "x86_64-unknown-linux-gnu"
49+
50+
@a = common global i8 0, align 8
51+
52+
;--- larger-sym.ll
2853
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
2954
target triple = "x86_64-unknown-linux-gnu"
3055

31-
@a = common global i32 0, align 8
56+
@a = common global i16 0, align 4
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
; REQUIRES: x86
2+
3+
;; Show that both regular LTO and ThinLTO work correctly when an input file uses
4+
;; a non-ascii filename.
5+
6+
;; Regular LTO.
7+
; RUN: llvm-as %s -o %t£.o
8+
; RUN: ld.lld %t£.o -o %t
9+
; RUN: llvm-readelf -s %t | FileCheck %s
10+
11+
;; Thin LTO.
12+
; RUN: opt -module-summary %s -o %t-thin£.o
13+
; RUN: ld.lld %t-thin£.o -o %t-thin
14+
; RUN: llvm-readelf -s %t-thin | FileCheck %s
15+
16+
; CHECK: _start
17+
18+
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
19+
target triple = "x86_64-unknown-linux-gnu"
20+
21+
@_start = global i32 0

lld/test/ELF/lto/resolution.ll

Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,53 @@
11
; REQUIRES: x86
2-
; RUN: llvm-as %s -o %t1.o
3-
; RUN: llvm-mc -triple=x86_64-pc-linux %p/Inputs/resolution.s -o %t2.o -filetype=obj
2+
;; Show that resolution of weak + global symbols works correctly when one is
3+
;; defined in native object and the other in a bitcode file.
4+
;; The global symbol in both cases should be kept. LTO should throw away the
5+
;; data for the discarded weak symbol defined in bitcode. The data for the
6+
;; weak symbol in a native object will be kept, but will be unlabelled.
7+
8+
; RUN: rm -rf %t.dir
9+
; RUN: split-file %s %t.dir
10+
; RUN: llvm-as %t.dir/1.ll -o %t1.o
11+
; RUN: llvm-mc -triple=x86_64-pc-linux %t.dir/2.s -o %t2.o -filetype=obj
412
; RUN: ld.lld %t1.o %t2.o -o %t.so -shared
5-
; RUN: llvm-readobj -S --section-data %t.so | FileCheck %s
13+
; RUN: llvm-readobj --symbols -S --section-data %t.so | FileCheck %s
614

715
; CHECK: Name: .data
816
; CHECK-NEXT: Type: SHT_PROGBITS
917
; CHECK-NEXT: Flags [
1018
; CHECK-NEXT: SHF_ALLOC
1119
; CHECK-NEXT: SHF_WRITE
1220
; CHECK-NEXT: ]
13-
; CHECK-NEXT: Address:
21+
; CHECK-NEXT: Address: 0x[[#%x,ADDR:]]
1422
; CHECK-NEXT: Offset:
15-
; CHECK-NEXT: Size: 4
16-
; CHECK-NEXT: Link: 0
17-
; CHECK-NEXT: Info: 0
18-
; CHECK-NEXT: AddressAlignment: 1
19-
; CHECK-NEXT: EntrySize: 0
23+
; CHECK-NEXT: Size: 12
24+
; CHECK-NEXT: Link:
25+
; CHECK-NEXT: Info:
26+
; CHECK-NEXT: AddressAlignment:
27+
; CHECK-NEXT: EntrySize:
2028
; CHECK-NEXT: SectionData (
21-
; CHECK-NEXT: 0000: 09000000 |{{.*}}|
29+
; CHECK-NEXT: 0000: 09000000 05000000 04000000 |{{.*}}|
2230
; CHECK-NEXT: )
2331

32+
; CHECK: Name: a{{ }}
33+
; CHECK-NEXT: Value: 0x[[#%x,ADDR]]
34+
35+
; CHECK: Name: b{{ }}
36+
; CHECK-NEXT: Value: 0x[[#%x,ADDR+8]]
37+
38+
;--- 1.ll
2439
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
2540
target triple = "x86_64-unknown-linux-gnu"
2641

2742
@a = weak global i32 8
43+
@b = global i32 4
44+
45+
;--- 2.s
46+
.data
47+
.global a
48+
a:
49+
.long 9
50+
51+
.weak b
52+
b:
53+
.long 5

0 commit comments

Comments
 (0)