1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
|
Linker scripts:
memtest_shared.lds
memtest_efi.lds
boot/
header.S || The standard EFI header
setup.S
efisetup.c || Macros + private functions
|| Only for 32-bit boot entry point???
startup64.S
*.c files in app/, lib/, system/, tests/
##########################################################################
## ##
## MEMTEST LINKING PART 1 - MEMTEST_SHARED ##
## ##
##########################################################################
#
# memtest_shared.lds
#
OUTPUT_FORMAT("elf64-x86-64")
ENTRY(startup64); <--------- in startup64.S
SECTIONS {
.text : {
_start = .; <---------- First section in memtest_shared-.text
*(.text)
*(.text.*)
...
.rodata
.dynsym
.dynstr
.hash
.gnu.hash
.dynamic
.rela.text
.rela.rodata
.rela.data
.rela.got
.rela.plt
.data
.got
.bss
#
# startup64.S
#
118: .globl startup64
119: startup64:
120: cld
121: cli
# Save the boot params pointer
125: movq %rsi, boot_params_addr(%rip)
127: jmp startup
143: .globl startup
144: startup:
Use startup stack until we pick a correct one
Pick the correct stack
Initialize the pml4 and pdp tables
Set the page directory base address
Initialise the GDT and the segment registers
Load the GDT and the segment registers
Initialise the IDT
Initialise the IDT descriptor
Zero the BSS (if first boot)
Initialise the FPU
Enable SSE
Call the dynamic linker to fix up the addresses in the GOT
268: call reloc <---------------- system/reloc64.c:151
get_load_address()
get_dynamic_section_offset()
get_dynamic_info()
do_relocations
276: call main <---------------- app/main.c:499 # main entry point called from the startup code
#
# Analysis of the binary
#
* objdump -h memtest_shared
.text ... File off Algn
00200000 2**4
* objdump -g memtest_shared | grep efi_setup # check for occurences
| grep efi_handover
* readelf -h memtest_shared
ELF Header:
Magic: 7f 45 4c 46 02 01 01 00 00 00 00 00 00 00 00 00
Class: ELF64
Data: 2's complement, little endian
Version: 1 (current)
OS/ABI: UNIX - System V
ABI Version: 0
Type: DYN (Shared object file)
Machine: Advanced Micro Devices X86-64
Version: 0x1
Entry point address: 0x200 <-----------------That's startup64 !!!
Start of program headers: 64 (bytes into file)
Start of section headers: 2821656 (bytes into file)
Flags: 0x0
Size of this header: 64 (bytes)
Size of program headers: 56 (bytes)
Number of program headers: 3
Size of section headers: 64 (bytes)
Number of section headers: 26
Section header string table index: 25
* objdump -d memtest_shared
memtest_shared: file format elf64-x86-64
Disassembly of section .text:
0000000000000000 <_start>:
0: cld
1: cli
9f: ljmp *-0x6(%rsp)
00000000000001e0 <efi_boot>:
1ed: jmp 210 <efi_handover>
0000000000000200 <startup64>:
202: mov %rsi,0x23df7(%rip) # 2400 <boot_params_addr>
209: jmp 220 <startup>
0000000000000210 <efi_handover>:
214: callq 68f <efi_setup>
219: mov %rax,0x23de0(%rip) # 2400 <boot_params_addr>
0000000000000220 <startup>:
2ce: ljmp *-0x6(%rsp)
##########################################################################
## ##
## MEMTEST LINKING PART 2 - MEMTEST.EFI ##
## ##
##########################################################################
#
# memtest_efi.lds
#
OUTPUT_FORMAT("binary")
ENTRY(boot); <----- in boot/header.S:36
SECTIONS {
.header : { <----- boot/header.S
*(.header)
}
.setup : { <----- boot/setup.S
*(.setup)
}
.text : {
_text_start = .;
*(.data) <----- !!! NO .text !!!!
...
#
# header.S
#
The EFI loader loads the header at ImageBase, so we have to locate the main program
after that. This means we can't load the main program at HIGH_LOAD_ADDR. Pick a load
address well away from HIGH_LOAD_ADDR, to avoid overlap when relocating code.
. # define IMAGE_BASE 0x200000
. # define BASE_OF_CODE 0x1000 <- where do these values come from?????
.section ".header", "ax", @progbits
.code16
.globl boot
boot:
"MZ", the MS-DOS header signature
.byte 0x4d
.byte 0x5a
pe_header:
coff_header:
...
#
# setup.S
#
Collects memory map information from the BIOS, disables APM, enables A20 and performs
the switch from real mode to protected mode before jumping to the main program entry
point.
The memory map information is stored in the 4KB block of memory immediately following
the setup code. The layout of the information matches the Linux boot_params struct.
A pointer to this block is passed to the main program, for compatibility with the Linux
32-bit boot protocol.
. # define BOOT_PARAMS_START (SETUP_SECS * 512) || SETUP_SEC is defined in boot.h
. # define BOOT_PARAMS_END (BOOT_PARAMS_START + 4096) <--- here are the 4K
.section ".setup", "ax", @progbits
.code16
Emulate the Linux boot header, to allow loading by other boot loaders. Indicate that the
main program code should be loaded in high memory.
.globl setup
setup: jmp do_setup
101: do_setup:
Reload the segment registers, except for the stack
Get the memory map and disable APM
Disable interrupts
Enable A20
...
179: flush:
Reload the segment registers and jump to the main test program <------------!!!!
...
188: jump:
data32 ljmp $KERNEL_CS, $0
389: Pad to the declared size
.org (ESTUP_SECS*512)
|