Skip to content

Commit f23554a

Browse files
committed
lld: Use PCC bounds from PT_CHERI_PCC segment for function caprelocs
1 parent df97a48 commit f23554a

File tree

7 files changed

+40
-19
lines changed

7 files changed

+40
-19
lines changed

lld/ELF/Arch/Cheri.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,13 @@ void CheriCapRelocsSection::writeToImpl(uint8_t *buf) {
393393
}
394394
}
395395

396+
// For function relocs, use PCC bounds from the PT_CHERI_PCC segment.
397+
if (config->emachine != EM_MIPS && (isFunc || isGnuIFunc)) {
398+
targetOffset += targetVA - pccBase();
399+
targetVA = pccBase();
400+
targetSize = pccSize();
401+
}
402+
396403
// TODO: should we warn about symbols that are out-of-bounds?
397404
// mandoc seems to do it so I guess we need it
398405
// if (TargetOffset < 0 || TargetOffset > TargetSize) warn(...);

lld/ELF/SyntheticSections.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4041,6 +4041,17 @@ void CheriPccPaddingSection::writeTo(uint8_t *buf) {
40414041
memset(buf, 0, size);
40424042
}
40434043

4044+
uint64_t elf::pccBase() {
4045+
return in.cheriBounds->firstSec->addr;
4046+
}
4047+
4048+
uint64_t elf::pccSize() {
4049+
PhdrEntry *phdr = in.cheriBounds;
4050+
OutputSection *first = phdr->firstSec;
4051+
OutputSection *last = phdr->lastSec;
4052+
return last->getVA() + last->size - first->getVA();
4053+
}
4054+
40444055
InStruct elf::in;
40454056

40464057
std::vector<Partition> elf::partitions;

lld/ELF/SyntheticSections.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1361,6 +1361,9 @@ struct InStruct {
13611361

13621362
LLVM_LIBRARY_VISIBILITY extern InStruct in;
13631363

1364+
uint64_t pccBase();
1365+
uint64_t pccSize();
1366+
13641367
} // namespace lld::elf
13651368

13661369
#endif

lld/test/ELF/cheri/caprelocs-surrounding-symbol.s

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ fn_reloc1:
7979
# EXE-RELOCS-NEXT: Location: 0x3480 (fn_reloc1)
8080
# EXE-RELOCS-NEXT: Base: fn1 (0x1370)
8181
# EXE-RELOCS-NEXT: Offset: 8
82-
# EXE-RELOCS-NEXT: Length: 12
82+
# EXE-RELOCS-NEXT: Length: 24
8383
# EXE-RELOCS-NEXT: Permissions: Function (0x8000000000000000)
8484
# EXE-RELOCS-NEXT: }
8585

@@ -102,9 +102,9 @@ fn_reloc2:
102102
# LLD-OUTPUT-NEXT: Found better match for capability relocation against .Lsecond_fn_target+0: .Lfn2$start+8
103103
# EXE-RELOCS-NEXT: Relocation {
104104
# EXE-RELOCS-NEXT: Location: 0x3490 (fn_reloc2)
105-
# EXE-RELOCS-NEXT: Base: .Lfn2$start (0x137C)
106-
# EXE-RELOCS-NEXT: Offset: 8
107-
# EXE-RELOCS-NEXT: Length: 12
105+
# EXE-RELOCS-NEXT: Base: fn1 (0x1370)
106+
# EXE-RELOCS-NEXT: Offset: 20
107+
# EXE-RELOCS-NEXT: Length: 24
108108
# EXE-RELOCS-NEXT: Permissions: Function (0x8000000000000000)
109109
# EXE-RELOCS-NEXT: }
110110
data_reloc1:

lld/test/ELF/cheri/exception-table.ll

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -102,14 +102,14 @@
102102
; RV64-RELOCS-OVERRIDE: [[#%.16x,TEST_WEAK_OVERRIDE_ADDR:]] 8 FUNC GLOBAL DEFAULT 9 _Z9test_weakll{{$}}
103103

104104
; RV64-RELOCS: CHERI __cap_relocs [
105-
; RV64-RELOCS-NEXT: 0x002{{.+}} Base: 0x[[#%x,TEST_ADDR]] (.L_Z4testll$local+92) Length: 116 Perms: Code
106-
; RV64-RELOCS-NEXT: 0x002{{.+}} Base: 0x[[#%x,TEST_ADDR]] (.L_Z4testll$local+72) Length: 116 Perms: Code
107-
; RV64-RELOCS-NEXT: 0x002{{.+}} Base: 0x[[#%x,TEST2_ADDR]] (.L_Z5test2ll$local+72) Length: 124 Perms: Code
105+
; RV64-RELOCS-NEXT: 0x002{{.+}} Base: 0x[[#%x,TEST_ADDR]] (.L_Z4testll$local+92) Length: 9184 Perms: Code
106+
; RV64-RELOCS-NEXT: 0x002{{.+}} Base: 0x[[#%x,TEST_ADDR]] (.L_Z4testll$local+72) Length: 9184 Perms: Code
107+
; RV64-RELOCS-NEXT: 0x002{{.+}} Base: 0x[[#%x,TEST_ADDR]] (.L_Z4testll$local+188) Length: 9184 Perms: Code
108108
; Next one references the local symbol, and uses that length rather than the override:
109-
; RV64-RELOCS-NEXT: 0x002{{.+}} Base: 0x[[#%x,TEST_WEAK_ADDR]] (.L_Z9test_weakll$local+28) Length: 52 Perms: Code
110-
; RV64-RELOCS-NEXT: 0x003{{.+}} Base: 0x[[#%x,PLT0_ADDR:]] (<unknown symbol>) Length: 80 Perms: Code
111-
; RV64-RELOCS-NEXT: 0x003{{.+}} Base: 0x[[#%x,PLT0_ADDR]] (<unknown symbol>) Length: 80 Perms: Code
112-
; RV64-RELOCS-NEXT: 0x003{{.+}} Base: 0x[[#%x,PLT0_ADDR]] (<unknown symbol>) Length: 80 Perms: Code
109+
; RV64-RELOCS-NEXT: 0x002{{.+}} Base: 0x[[#%x,TEST_ADDR]] (.L_Z4testll$local+268) Length: 9184 Perms: Code
110+
; RV64-RELOCS-NEXT: 0x003{{.+}} Base: 0x[[#%x,TEST_ADDR]] (.L_Z4testll$local+304) Length: 9184 Perms: Code
111+
; RV64-RELOCS-NEXT: 0x003{{.+}} Base: 0x[[#%x,TEST_ADDR]] (.L_Z4testll$local+304) Length: 9184 Perms: Code
112+
; RV64-RELOCS-NEXT: 0x003{{.+}} Base: 0x[[#%x,TEST_ADDR]] (.L_Z4testll$local+304) Length: 9184 Perms: Code
113113
; RV64-RELOCS-NEXT: ]
114114

115115
; IR was generated from the following code:

lld/test/ELF/cheri/riscv/ifunc-nonpreemptible.s

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,17 @@
2121
# RELOC32: Relocations [
2222
# RELOC32-NEXT: ]
2323
# RELOC32: CHERI __cap_relocs [
24-
# RELOC32-NEXT: 0x012000 (fptr) Base: 0x11010 (func) Length: 16 Perms: Function
25-
# RELOC32-NEXT: 0x012008 Base: 0x11000 (<unknown symbol>) Length: 4 Perms: GNU Indirect Function
24+
# RELOC32-NEXT: 0x012000 (fptr) Base: 0x11000 (func) Length: 4608 Perms: Function
25+
# RELOC32-NEXT: 0x012008 Base: 0x11000 (<unknown symbol>) Length: 4608 Perms: GNU Indirect Function
2626
# RELOC32-NEXT: ]
2727
# GOTPLT32: section '.got.plt':
2828
# GOTPLT32-NEXT: 0x00012008 00000000 00000000
2929

3030
# RELOC64: Relocations [
3131
# RELOC64-NEXT: ]
3232
# RELOC64: CHERI __cap_relocs [
33-
# RELOC64-NEXT: 0x012000 (fptr) Base: 0x11010 (func) Length: 16 Perms: Function
34-
# RELOC64-NEXT: 0x012010 Base: 0x11000 (<unknown symbol>) Length: 4 Perms: GNU Indirect Function
33+
# RELOC64-NEXT: 0x012000 (fptr) Base: 0x11000 (func) Length: 4128 Perms: Function
34+
# RELOC64-NEXT: 0x012010 Base: 0x11000 (<unknown symbol>) Length: 4128 Perms: GNU Indirect Function
3535
# RELOC64-NEXT: ]
3636
# GOTPLT64: section '.got.plt':
3737
# GOTPLT64-NEXT: 0x00012010 00000000 00000000 00000000 00000000

lld/test/ELF/cheri/riscv/plt.s

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@
3131
# RELOC32-NEXT: 0x13090 R_RISCV_JUMP_SLOT weak 0x0
3232
# RELOC32-NEXT: }
3333
# RELOC32: CHERI __cap_relocs [
34-
# RELOC32-NEXT: 0x013088 Base: 0x11030 (<unknown symbol>) Length: 64 Perms: Code
35-
# RELOC32-NEXT: 0x013090 Base: 0x11030 (<unknown symbol>) Length: 64 Perms: Code
34+
# RELOC32-NEXT: 0x013088 Base: 0x11000 (_start+48) Length: 9216 Perms: Code
35+
# RELOC32-NEXT: 0x013090 Base: 0x11000 (_start+48) Length: 9216 Perms: Code
3636
# RELOC32-NEXT: ]
3737
# GOTPLT32: section '.got.plt'
3838
# GOTPLT32-NEXT: 0x00013078 00000000 00000000 00000000 00000000
@@ -43,8 +43,8 @@
4343
# RELOC64-NEXT: 0x13120 R_RISCV_JUMP_SLOT weak 0x0
4444
# RELOC64-NEXT: }
4545
# RELOC64: CHERI __cap_relocs [
46-
# RELOC64-NEXT: 0x013110 Base: 0x11030 (<unknown symbol>) Length: 64 Perms: Code
47-
# RELOC64-NEXT: 0x013120 Base: 0x11030 (<unknown symbol>) Length: 64 Perms: Code
46+
# RELOC64-NEXT: 0x013110 Base: 0x11000 (_start+48) Length: 8496 Perms: Code
47+
# RELOC64-NEXT: 0x013120 Base: 0x11000 (_start+48) Length: 8496 Perms: Code
4848
# RELOC64-NEXT: ]
4949
# GOTPLT64: section '.got.plt'
5050
# GOTPLT64-NEXT: 0x000130f0 00000000 00000000 00000000 00000000

0 commit comments

Comments
 (0)