1
- # RUN: yaml2obj %s -o %t
2
- # RUN: llvm-objcopy -O binary %t %t2
3
- # RUN: od -t x2 %t2 | FileCheck %s --ignore-case
4
- # RUN: wc -c < %t2 | FileCheck %s --check-prefix=SIZE
1
+ ## The computed LMA of a section in a PT_LOAD equals sh_offset-p_offset+p_paddr.
2
+ ## The byte offset difference between two sections equals the difference between their LMAs.
5
3
6
- !ELF
4
+ ## Corollary: if two sections are in the same PT_LOAD, the byte offset
5
+ ## difference equals the difference between their sh_addr fields.
6
+
7
+ # RUN: yaml2obj --docnum=1 %s -o %t1
8
+ # RUN: llvm-objcopy -O binary %t1 %t1.out
9
+ # RUN: od -A x -t x2 %t1.out | FileCheck %s --check-prefix=CHECK1 --ignore-case
10
+ # RUN: wc -c %t1.out | FileCheck %s --check-prefix=SIZE1
11
+
12
+ # CHECK1: 000000 c3c3 c3c3 0000 0000 0000 0000 0000 0000
13
+ # CHECK1-NEXT: 000010 0000 0000 0000 0000 0000 0000 0000 0000
14
+ # CHECK1-NEXT: *
15
+ # CHECK1-NEXT: 001000 3232
16
+ # SIZE1: 4098
17
+
18
+ --- !ELF
7
19
FileHeader:
8
20
Class: ELFCLASS64
9
21
Data: ELFDATA2LSB
@@ -14,32 +26,107 @@ Sections:
14
26
Type: SHT_PROGBITS
15
27
Flags: [ SHF_ALLOC, SHF_EXECINSTR ]
16
28
Address: 0x1000
17
- AddressAlign: 0x0000000000001000
29
+ AddressAlign: 0x1000
18
30
Content: "c3c3c3c3"
19
31
- Name: .data
20
32
Type: SHT_PROGBITS
21
- Flags: [ SHF_ALLOC ]
33
+ Flags: [ SHF_ALLOC, SHF_WRITE ]
22
34
Address: 0x2000
23
- AddressAlign: 0x0000000000001000
35
+ AddressAlign: 0x1000
24
36
Content: "3232"
25
37
ProgramHeaders:
26
38
- Type: PT_LOAD
27
- Flags: [ PF_X, PF_R ]
28
- VAddr: 0x1000
29
- PAddr: 0x1000
30
- Align: 0x1000
39
+ Flags: [ PF_R, PF_W ]
31
40
Sections:
32
41
- Section: .text
42
+ - Section: .data
43
+
44
+ ## The computed LMA of a section not in a PT_LOAD equals its sh_addr.
45
+
46
+ # RUN: yaml2obj --docnum=2 %s -o %t2
47
+ # RUN: llvm-objcopy -O binary %t2 %t2.out
48
+ # RUN: od -A x -t x2 %t2.out | FileCheck %s --check-prefix=CHECK2 --ignore-case
49
+ # RUN: wc -c %t2.out | FileCheck %s --check-prefix=SIZE2
50
+
51
+ ## The computed LMA of .data is 0x4000. The minimum LMA of all sections is 0x1000.
52
+ ## The content of .data will be written at 0x4000-0x1000 = 0x3000.
53
+ # CHECK2: 000000 c3c3 c3c3 0000 0000 0000 0000 0000 0000
54
+ # CHECK2-NEXT: 000010 0000 0000 0000 0000 0000 0000 0000 0000
55
+ # CHECK2-NEXT: *
56
+ # CHECK2-NEXT: 003000 3232
57
+ # SIZE2: 12290
58
+
59
+ --- !ELF
60
+ FileHeader:
61
+ Class: ELFCLASS64
62
+ Data: ELFDATA2LSB
63
+ Type: ET_EXEC
64
+ Machine: EM_X86_64
65
+ Sections:
66
+ - Name: .text
67
+ Type: SHT_PROGBITS
68
+ Flags: [ SHF_ALLOC, SHF_EXECINSTR ]
69
+ ## Not in a PT_LOAD. LMA = sh_addr = 0x1000.
70
+ Address: 0x1000
71
+ AddressAlign: 0x1000
72
+ Content: "c3c3c3c3"
73
+ - Name: .data
74
+ Type: SHT_PROGBITS
75
+ Flags: [ SHF_ALLOC, SHF_WRITE ]
76
+ ## LMA = sh_offset-p_offset+p_paddr = 0x2000-0x2000+0x4000 = 0x4000.
77
+ Address: 0x2000
78
+ AddressAlign: 0x1000
79
+ Content: "3232"
80
+ ProgramHeaders:
33
81
- Type: PT_LOAD
34
82
Flags: [ PF_R, PF_W ]
35
83
VAddr: 0x2000
84
+ ## p_vaddr is increased from 0x2000 to 0x4000.
36
85
PAddr: 0x4000
37
- Align: 0x1000
38
86
Sections:
39
87
- Section: .data
40
88
41
- # CHECK: 0000000 c3c3 c3c3 0000 0000 0000 0000 0000 0000
42
- # CHECK-NEXT: 0000020 0000 0000 0000 0000 0000 0000 0000 0000
43
- # CHECK-NEXT: *
44
- # CHECK-NEXT: 0030000 3232
45
- # SIZE: 12290
89
+ ## Check that we use sh_offset instead of sh_addr to decide where to write section contents.
90
+
91
+ # RUN: yaml2obj --docnum=3 %s -o %t3
92
+ # RUN: llvm-objcopy -O binary %t3 %t3.out
93
+ # RUN: od -A x -t x2 %t3.out | FileCheck %s --check-prefix=CHECK3 --ignore-case
94
+ # RUN: wc -c %t3.out | FileCheck %s --check-prefix=SIZE3
95
+
96
+ ## The minimum LMA of all sections is 0x1000.
97
+ ## The content of .data will be written at 0x3000-0x1000 = 0x2000.
98
+ # CHECK3: 000000 c3c3 c3c3 0000 0000 0000 0000 0000 0000
99
+ # CHECK3-NEXT: 000010 0000 0000 0000 0000 0000 0000 0000 0000
100
+ # CHECK3-NEXT: *
101
+ # CHECK3-NEXT: 002000 3232
102
+ # SIZE3: 8194
103
+
104
+ --- !ELF
105
+ FileHeader:
106
+ Class: ELFCLASS64
107
+ Data: ELFDATA2LSB
108
+ Type: ET_EXEC
109
+ Machine: EM_X86_64
110
+ Sections:
111
+ - Name: .text
112
+ Type: SHT_PROGBITS
113
+ Flags: [ SHF_ALLOC, SHF_EXECINSTR ]
114
+ ## Not in a PT_LOAD. LMA = sh_addr = 0x1000.
115
+ Address: 0x1000
116
+ AddressAlign: 0x1000
117
+ Content: "c3c3c3c3"
118
+ - Name: .data
119
+ Type: SHT_PROGBITS
120
+ Flags: [ SHF_ALLOC, SHF_WRITE ]
121
+ ## sh_addr is increased from 0x2000 to 0x3000, but it is ignored.
122
+ ## LMA = sh_offset-p_offset+p_paddr = 0x2000-0x2000+0x3000 = 0x3000.
123
+ Address: 0x3000
124
+ AddressAlign: 0x1000
125
+ Content: "3232"
126
+ ProgramHeaders:
127
+ - Type: PT_LOAD
128
+ Flags: [ PF_R, PF_W ]
129
+ VAddr: 0x3000
130
+ PAddr: 0x3000
131
+ Sections:
132
+ - Section: .data
0 commit comments