1# REQUIRES: x86
2# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t.o
3
4# RUN: echo "SECTIONS { \
5# RUN:   .sec1 0x8000 : AT(0x8000) { sec1_start = .; *(.first_sec) sec1_end = .;} \
6# RUN:   .sec2 0x8800 : AT(0x8080) { sec2_start = .; *(.second_sec) sec2_end = .;} \
7# RUN: }" > %t-lma.script
8# RUN: not ld.lld -o /dev/null -T %t-lma.script %t.o -shared --no-rosegment 2>&1 | FileCheck %s -check-prefix LMA-OVERLAP-ERR
9# LMA-OVERLAP-ERR:      error: section .sec1 load address range overlaps with .sec2
10# LMA-OVERLAP-ERR-NEXT: >>> .sec1 range is [0x8000, 0x80FF]
11# LMA-OVERLAP-ERR-NEXT: >>> .sec2 range is [0x8080, 0x817F]
12
13# Check that we create the expected binary with --noinhibit-exec or --no-check-sections:
14# RUN: ld.lld -o %t.so --script %t-lma.script %t.o -shared --no-rosegment --noinhibit-exec
15# RUN: ld.lld -o %t.so --script %t-lma.script %t.o -shared --no-rosegment --no-check-sections -fatal-warnings
16# RUN: ld.lld -o %t.so --script %t-lma.script %t.o -shared --no-rosegment --check-sections --no-check-sections -fatal-warnings
17
18# Verify that the .sec2 was indeed placed in a PT_LOAD where the PhysAddr
19# overlaps with where .sec1 is loaded:
20# RUN: llvm-readelf --sections -l %t.so | FileCheck %s -check-prefix BAD-LMA
21# BAD-LMA-LABEL: Section Headers:
22# BAD-LMA: .sec1             PROGBITS        0000000000008000 002000 000100 00  WA  0   0  1
23# BAD-LMA: .sec2             PROGBITS        0000000000008800 002800 000100 00  WA  0   0  1
24# BAD-LMA-LABEL: Program Headers:
25# BAD-LMA-NEXT:  Type           Offset   VirtAddr           PhysAddr           FileSiz  MemSiz   Flg Align
26# BAD-LMA-NEXT:  LOAD           0x001000 0x0000000000000000 0x0000000000000000 0x000100 0x000100 R E 0x1000
27# BAD-LMA-NEXT:  LOAD           0x001100 0x0000000000000100 0x0000000000000100 0x000070 0x000070 RW  0x1000
28# BAD-LMA-NEXT:  LOAD           0x002000 0x0000000000008000 0x0000000000008000 0x000100 0x000100 RW  0x1000
29# BAD-LMA-LABEL: Section to Segment mapping:
30# BAD-LMA:  01     .text .dynamic
31# BAD-LMA:  02     .sec1
32
33# Now try a script where the virtual memory addresses overlap but ensure that the
34# load addresses don't:
35# RUN: echo "SECTIONS { \
36# RUN:   .sec1 0x8000 : AT(0x8000) { sec1_start = .; *(.first_sec) sec1_end = .;} \
37# RUN:   .sec2 0x8020 : AT(0x8800) { sec2_start = .; *(.second_sec) sec2_end = .;} \
38# RUN: }" > %t-vaddr.script
39# RUN: not ld.lld -o /dev/null --script %t-vaddr.script %t.o -shared 2>&1 | FileCheck %s -check-prefix VADDR-OVERLAP-ERR
40# VADDR-OVERLAP-ERR:      error: section .sec1 virtual address range overlaps with .sec2
41# VADDR-OVERLAP-ERR-NEXT: >>> .sec1 range is [0x8000, 0x80FF]
42# VADDR-OVERLAP-ERR-NEXT: >>> .sec2 range is [0x8020, 0x811F]
43
44# Check that the expected binary was created with --noinhibit-exec:
45# RUN: ld.lld -o %t.so --script %t-vaddr.script %t.o -shared --no-rosegment --noinhibit-exec
46# RUN: llvm-readelf --sections -l %t.so | FileCheck %s -check-prefix BAD-VADDR
47# BAD-VADDR-LABEL: Section Headers:
48# BAD-VADDR: .sec1             PROGBITS        0000000000008000 002000 000100 00  WA  0   0  1
49# BAD-VADDR: .sec2             PROGBITS        0000000000008020 003020 000100 00  WA  0   0  1
50# BAD-VADDR-LABEL: Program Headers:
51# BAD-VADDR-NEXT:  Type           Offset   VirtAddr           PhysAddr           FileSiz  MemSiz   Flg Align
52# BAD-VADDR-NEXT:  LOAD           0x001000 0x0000000000000000 0x0000000000000000 0x000100 0x000100 R E 0x1000
53# BAD-VADDR-NEXT:  LOAD           0x001100 0x0000000000000100 0x0000000000000100 0x000070 0x000070 RW  0x1000
54# BAD-VADDR-NEXT:  LOAD           0x002000 0x0000000000008000 0x0000000000008000 0x000100 0x000100 RW  0x1000
55# BAD-VADDR-LABEL: Section to Segment mapping:
56# BAD-VADDR:  01     .text .dynamic
57# BAD-VADDR:  02     .sec1
58
59# Finally check the case where both LMA and vaddr overlap
60
61# RUN: echo "SECTIONS { \
62# RUN:   .sec1 0x8000 : { sec1_start = .; *(.first_sec) sec1_end = .;} \
63# RUN:   .sec2 0x8040 : { sec2_start = .; *(.second_sec) sec2_end = .;} \
64# RUN: }" > %t-both-overlap.script
65
66# RUN: not ld.lld -o /dev/null --script %t-both-overlap.script %t.o -shared 2>&1 | FileCheck %s -check-prefix BOTH-OVERLAP-ERR
67
68# BOTH-OVERLAP-ERR:      error: section .sec1 file range overlaps with .sec2
69# BOTH-OVERLAP-ERR-NEXT: >>> .sec1 range is [0x2000, 0x20FF]
70# BOTH-OVERLAP-ERR-NEXT: >>> .sec2 range is [0x2040, 0x213F]
71# BOTH-OVERLAP-ERR:      error: section .sec1 virtual address range overlaps with .sec2
72# BOTH-OVERLAP-ERR-NEXT: >>> .sec1 range is [0x8000, 0x80FF]
73# BOTH-OVERLAP-ERR-NEXT: >>> .sec2 range is [0x8040, 0x813F]
74# BOTH-OVERLAP-ERR:      error: section .sec1 load address range overlaps with .sec2
75# BOTH-OVERLAP-ERR-NEXT: >>> .sec1 range is [0x8000, 0x80FF]
76# BOTH-OVERLAP-ERR-NEXT: >>> .sec2 range is [0x8040, 0x813F]
77
78# RUN: ld.lld -o %t.so --script %t-both-overlap.script %t.o -shared --no-rosegment --noinhibit-exec
79# Note: In case everything overlaps we create a binary with overlapping file
80# offsets. ld.bfd seems to place .sec1 to file offset 18000 and .sec2
81# at 18100 so that only virtual addr and LMA overlap
82# However, in order to create such a broken binary the user has to ignore a
83# fatal error by passing --noinhibit-exec, so this behaviour is fine.
84
85# RUN: llvm-objdump -s %t.so | FileCheck %s --check-prefix BROKEN-OUTPUT-FILE
86# BROKEN-OUTPUT-FILE-LABEL: Contents of section .sec1:
87# BROKEN-OUTPUT-FILE-NEXT: 8000 01010101 01010101 01010101 01010101
88# BROKEN-OUTPUT-FILE-NEXT: 8010 01010101 01010101 01010101 01010101
89# BROKEN-OUTPUT-FILE-NEXT: 8020 01010101 01010101 01010101 01010101
90# BROKEN-OUTPUT-FILE-NEXT: 8030 01010101 01010101 01010101 01010101
91# Starting here the contents of .sec2 overwrites .sec1:
92# BROKEN-OUTPUT-FILE-NEXT: 8040 02020202 02020202 02020202 02020202
93
94# RUN: llvm-readelf --sections -l %t.so | FileCheck %s -check-prefix BAD-BOTH
95# BAD-BOTH-LABEL: Section Headers:
96# BAD-BOTH: .sec1             PROGBITS        0000000000008000 002000 000100 00  WA  0   0  1
97# BAD-BOTH: .sec2             PROGBITS        0000000000008040 002040 000100 00  WA  0   0  1
98# BAD-BOTH-LABEL: Program Headers:
99# BAD-BOTH-NEXT:  Type           Offset   VirtAddr           PhysAddr           FileSiz  MemSiz   Flg Align
100# BAD-BOTH-NEXT:  LOAD 0x001000 0x0000000000000000 0x0000000000000000 0x000100 0x000100 R E 0x1000
101# BAD-BOTH-NEXT:  LOAD           0x001100 0x0000000000000100 0x0000000000000100 0x000070 0x000070 RW  0x1000
102# BAD-BOTH-LABEL: Section to Segment mapping:
103# BAD-BOTH:   01     .text .dynamic
104
105.section        .first_sec,"aw",@progbits
106.rept 0x100
107.byte 1
108.endr
109
110.section        .second_sec,"aw",@progbits
111.rept 0x100
112.byte 2
113.endr
114