Skip to content

Commit a8d6c6b

Browse files
committed
RISC-V support
1 parent 7682eba commit a8d6c6b

File tree

3 files changed

+140
-3
lines changed

3 files changed

+140
-3
lines changed

GccBase.lds

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/** @file
2+
3+
Unified linker script for GCC based builds
4+
5+
Copyright (c) 2010 - 2015, Intel Corporation. All rights reserved.<BR>
6+
Copyright (c) 2015, Linaro Ltd. All rights reserved.<BR>
7+
(C) Copyright 2016 Hewlett Packard Enterprise Development LP<BR>
8+
9+
SPDX-License-Identifier: BSD-2-Clause-Patent
10+
11+
**/
12+
13+
SECTIONS {
14+
15+
/*
16+
* The PE/COFF binary consists of DOS and PE/COFF headers, and a sequence of
17+
* section headers adding up to PECOFF_HEADER_SIZE bytes (which differs
18+
* between 32-bit and 64-bit builds). The actual start of the .text section
19+
* will be rounded up based on its actual alignment.
20+
*/
21+
. = PECOFF_HEADER_SIZE;
22+
23+
.text : ALIGN(COMMONPAGESIZE) {
24+
*(.text .text.* .stub .gnu.linkonce.t.*)
25+
*(.rodata .rodata.* .gnu.linkonce.r.*)
26+
*(.got .got.*)
27+
28+
/*
29+
* The contents of AutoGen.c files are mostly constant from the POV of the
30+
* program, but most of it ends up in .data or .bss by default since few of
31+
* the variable definitions that get emitted are declared as CONST.
32+
* Unfortunately, we cannot pull it into the .text section entirely, since
33+
* patchable PCDs are also emitted here, but we can at least move all of the
34+
* emitted GUIDs here.
35+
*/
36+
*:AutoGen.obj(.data.g*Guid)
37+
}
38+
39+
/*
40+
* The alignment of the .data section should be less than or equal to the
41+
* alignment of the .text section. This ensures that the relative offset
42+
* between these sections is the same in the ELF and the PE/COFF versions of
43+
* this binary.
44+
*/
45+
.data ALIGN(ALIGNOF(.text)) : ALIGN(COMMONPAGESIZE) {
46+
*(.data .data.* .gnu.linkonce.d.*)
47+
*(.bss .bss.*)
48+
}
49+
50+
.rela (INFO) : {
51+
*(.rela .rela.*)
52+
}
53+
54+
.hii : ALIGN(COMMONPAGESIZE) {
55+
KEEP (*(.hii))
56+
}
57+
58+
/*
59+
* Retain the GNU build id but in a non-allocatable section so GenFw
60+
* does not copy it into the PE/COFF image.
61+
*/
62+
.build-id (INFO) : { *(.note.gnu.build-id) }
63+
64+
/DISCARD/ : {
65+
*(.eh_frame)
66+
*(.note.GNU-stack)
67+
*(.gnu_debuglink)
68+
*(.interp)
69+
*(.dynsym)
70+
*(.dynstr)
71+
*(.dynamic)
72+
*(.hash .gnu.hash)
73+
*(.comment)
74+
}
75+
}

build.py

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
'qemu_binary': {
3838
'x86_64': 'qemu-system-x86_64',
3939
'aarch64': 'qemu-system-aarch64',
40+
'riscv64': 'qemu-system-riscv64',
4041
},
4142
# Path to directory containing `OVMF_{CODE/VARS}.fd` (for x86_64),
4243
# or `*-pflash.raw` (for AArch64).
@@ -61,6 +62,14 @@ def get_target_triple():
6162
arch = SETTINGS['arch']
6263
return f'{arch}-unknown-uefi'
6364

65+
def get_target_triple_option():
66+
arch = SETTINGS['arch']
67+
if arch == 'riscv64':
68+
triple = 'riscv64-unknown-uefi.json'
69+
else:
70+
triple = get_target_triple()
71+
return triple
72+
6473
def build_dir():
6574
'Returns the directory where Cargo places the build artifacts'
6675
return target_dir() / get_target_triple() / SETTINGS['config']
@@ -72,7 +81,7 @@ def esp_dir():
7281
def run_tool(tool, *flags):
7382
'Runs cargo-<tool> with certain arguments.'
7483

75-
target = get_target_triple()
84+
target = get_target_triple_option()
7685
cmd = ['cargo', tool, '--target', target, *flags]
7786

7887
if SETTINGS['verbose']:
@@ -107,14 +116,23 @@ def build(*test_flags):
107116
# Copy the built test runner file to the right directory for running tests.
108117
built_file = build_dir() / 'uefi-test-runner.efi'
109118

119+
arch = SETTINGS['arch']
120+
if arch == 'riscv64':
121+
sp.run([
122+
'elftoefi',
123+
build_dir() / 'uefi-test-runner',
124+
built_file,
125+
], check=True)
126+
110127
boot_dir = esp_dir() / 'EFI' / 'Boot'
111128
boot_dir.mkdir(parents=True, exist_ok=True)
112129

113-
arch = SETTINGS['arch']
114130
if arch == 'x86_64':
115131
output_file = boot_dir / 'BootX64.efi'
116132
elif arch == 'aarch64':
117133
output_file = boot_dir / 'BootAA64.efi'
134+
elif arch == 'riscv64':
135+
output_file = boot_dir / 'BootRV64.efi'
118136

119137
shutil.copy2(built_file, output_file)
120138

@@ -302,6 +320,11 @@ def run_qemu():
302320
# A72 is a very generic 64-bit ARM CPU in the wild
303321
'-cpu', 'cortex-a72',
304322
])
323+
elif arch == 'riscv64':
324+
qemu_flags.extend([
325+
'-machine', 'virt',
326+
'-cpu', 'rv64',
327+
])
305328
else:
306329
raise NotImplementedError('Unknown arch')
307330

@@ -434,7 +457,7 @@ def main():
434457
choices=['build', 'run', 'doc', 'clippy', 'test'])
435458

436459
parser.add_argument('--target', help='target to build for (default: %(default)s)', type=str,
437-
choices=['x86_64', 'aarch64'], default='x86_64')
460+
choices=['x86_64', 'aarch64', 'riscv64'], default='x86_64')
438461

439462
parser.add_argument('--verbose', '-v', help='print commands before executing them',
440463
action='store_true')

riscv64-unknown-uefi.json

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
{
2+
"abi-return-struct-as-int": true,
3+
"allows-weak-linkage": false,
4+
"arch": "riscv64",
5+
"code-model": "medium",
6+
"cpu": "generic-rv64",
7+
"data-layout": "e-m:e-p:64:64-i64:64-i128:128-n64-S128",
8+
"disable-redzone": true,
9+
"eh-frame-header": false,
10+
"emit-debug-gdb-scripts": false,
11+
"executables": true,
12+
"features": "+m,+a,+f,+d,+c",
13+
"linker": "rust-lld",
14+
"linker-flavor": "ld.lld",
15+
"llvm-target": "riscv64",
16+
"max-atomic-width": 64,
17+
"os": "uefi",
18+
"panic-strategy": "abort",
19+
"pre-link-args": {
20+
"ld.lld": [
21+
"--entry", "efi_main",
22+
"--nmagic",
23+
"--emit-relocs",
24+
"--no-relax",
25+
"-z", "common-page-size=0x40",
26+
"--defsym=COMMONPAGESIZE=0x40",
27+
"--defsym=PECOFF_HEADER_SIZE=0x220",
28+
"-TGccBase.lds"
29+
]
30+
},
31+
"singlethread": true,
32+
"split-debuginfo": "packed",
33+
"stack-probes": {
34+
"kind": "call"
35+
},
36+
"target-c-int-width": "32",
37+
"target-endian": "little",
38+
"target-pointer-width": "64"
39+
}

0 commit comments

Comments
 (0)