Skip to content

Commit

Permalink
App loading with more features
Browse files Browse the repository at this point in the history
1. Relocations are performed, obviating the need for manual `pic`
   sprinkling.

2. Globals with initial values (vs all-0 globals in BSS) are now
   initialized to those values.
  • Loading branch information
Ericson2314 committed Feb 13, 2023
1 parent 3058db1 commit abbc6d4
Show file tree
Hide file tree
Showing 7 changed files with 330 additions and 22 deletions.
5 changes: 4 additions & 1 deletion .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ jobs:
toolchain: nightly
override: true
components: rust-src
- run: echo "/rsdk/scripts" >> $GITHUB_PATH
- uses: actions/checkout@v2
- name: Cargo build
uses: actions-rs/cargo@v1
Expand All @@ -93,11 +94,13 @@ jobs:
release: '9-2019-q4'
- name: Install clang
run: sudo apt-get update && sudo apt install -y clang
- uses: actions-rs/toolchain@v1
- name: Install toolchains
uses: actions-rs/toolchain@v1
with:
toolchain: nightly
override: true
components: rust-src
- run: echo "$PWD/scripts" >> $GITHUB_PATH
- name: Install dependencies
run: |
sudo apt-get update && sudo apt-get install -y qemu-user-static
Expand Down
122 changes: 106 additions & 16 deletions link.ld
Original file line number Diff line number Diff line change
@@ -1,43 +1,118 @@
PHDRS
{
flash0 PT_LOAD ;
data PT_LOAD ;
flash2 PT_LOAD ;
sram PT_LOAD ;

headers PT_PHDR PHDRS ;
}

SECTIONS
{
/* Code, read only, no relocations needed. */
.text :
{
_text = .;
/* Here begins flash. This symbol is used by the ideompotent `pic`
function as the lower bound of addressed to relocate. */
_nvram_start = .;

_text = .;

*(.boot*)
*(.text*)
*(.rodata*)

/* .rodata is moved out so we can update it */
. = ALIGN(PAGE_SIZE);

_etext = .;
} > FLASH
} > FLASH :flash0
_text_len = _etext - _text;

.nvm_data : ALIGN(PAGE_SIZE)
/* Relocations, read only, no relocations aginst the relocations themselves
needed! */
.rel_flash : ALIGN(PAGE_SIZE)
{
_relocs = .;

/* define _reloc_size in the build.rs */
. += _reloc_size;
/*(.rel.rodata .rel.rodata*)*/
/*(.rel.data .rel.data*)*/
/*(.rel.nvm_data .rel.nvm_data*)*/
. = ALIGN(PAGE_SIZE);

_erelocs = .;

. = ALIGN(PAGE_SIZE);

/* After this section we have mutable flash. Must be a multiple of PAGE_SIZE from _nvram_start. */
_nvram_data = .;
*(.nvm_data*)
} > FLASH :flash0
_relocs_len = _erelocs - _relocs;

/* Immutable globals, read only during app running proper, but
relocations are needed. (So not read-only completely.) */
.rodata : ALIGN(PAGE_SIZE)
{
/* Moved here from .text so we can permantly apply relocations to it with
nvm_write() */
. = ALIGN(PAGE_SIZE);
_envram_data = .;
_install_parameters = .;
_nvram_end = .;
} > FLASH
_rodata = .;
_rodata_src = .;
*(.rodata*)
/**(.rodata*)*/
. = ALIGN(PAGE_SIZE);
_erodata = .;
} > FLASH :flash0
_rodata_len = _erodata - _rodata;

.data : ALIGN(4)
/* Mutable Globals, writable, relocations are needed. */
.data : ALIGN(PAGE_SIZE)
{
_data = .;
*(vtable)
*(.data*)
. = ALIGN(PAGE_SIZE);
_edata = .;
} > SRAM
FILL(0xa4a4);
} > SRAM AT> FLASH :data =0xa4a4
_data_len = SIZEOF(.data); /* _edata - _data; */

/* Persistent data, read and written during app running proper,
relocations are also needed. */
.nvm_data : ALIGN(PAGE_SIZE)
{
_nvm_data = .;
_nvm_data_src = .;
*(.nvm_data*)
. = ALIGN(PAGE_SIZE);
_envm_data = .;

/* Debugging: Add some extra buffer space just to make sure we don't cloober install_parameters */
. += PAGE_SIZE*4;
/* and make sure we're on a page boundary for them */
. = ALIGN(PAGE_SIZE);
_install_parameters = .;

/* After this section we no longer have Flash memory at all. */

/* This symbol is used by the mutable portion of flash calculations. */
_envram_data = .;
/* This symbol is used by the ideompotent `pic` function as the upper
bound of addressed to relocate. */
_nvram_end = .;
} > FLASH :flash2
_nvm_data_len = _envm_data - _nvm_data;

ASSERT( (_edata - _data) <= 0, ".data section must be empty" )
_sidata = LOADADDR(.data);
_sidata_src = LOADADDR(.data);

.bss :
.bss :
{
_bss = .;
*(.bss*)
_ebss = .;
_bss_len = ABSOLUTE(_ebss) - ABSOLUTE(_bss);

. = ALIGN(4);
app_stack_canary = .;
Expand All @@ -46,19 +121,34 @@ SECTIONS
. = _stack_validation + STACK_SIZE;
_stack = ABSOLUTE(END_STACK) - STACK_SIZE;
_estack = ABSOLUTE(END_STACK);
} > SRAM
} > SRAM :sram

.stack_sizes (INFO):
{
KEEP(*(.stack_sizes));
}
} :NONE

/*
.rel (INFO):
{
KEEP(*(.rel.*));
} :NONE
*/

/*.text.debug_info (INFO):
{
*(.text.debug_info)
}*/

/DISCARD/ :
{
libc.a ( * )
libm.a ( * )
libgcc.a ( * )
*(.ARM.exidx* .gnu.linkonce.armexidx.*)
*(.ARM.extab*)
/* *(.rel.debug_info)
*(.rela.debug_info) */
*(.debug_info)
}
}
Expand Down
4 changes: 2 additions & 2 deletions nanos.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
"atomic-cas": false,
"c-enum-min-bits": 8,
"data-layout": "e-m:e-p:32:32-Fi8-i64:64-v128:64:128-a:0:32-n32-S64",
"emit-debug-gdb-scripts": false,
"emit-debug-gdb-scripts": true,
"executables": true,
"features": "+strict-align",
"frame-pointer": "always",
"linker": "rust-lld",
"linker": "link_wrap.sh",
"linker-flavor": "ld.lld",
"llvm-target": "thumbv6m-none-eabi",
"panic-strategy": "abort",
Expand Down
2 changes: 1 addition & 1 deletion nanosplus.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"emit-debug-gdb-scripts": false,
"executables": true,
"frame-pointer": "always",
"linker": "rust-lld",
"linker": "link_wrap.sh",
"linker-flavor": "ld.lld",
"llvm-target": "thumbv8m.main-none-eabi",
"max-atomic-width": 32,
Expand Down
3 changes: 1 addition & 2 deletions nanox.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"emit-debug-gdb-scripts": false,
"executables": true,
"frame-pointer": "always",
"linker": "rust-lld",
"linker": "link_wrap.sh",
"linker-flavor": "ld.lld",
"llvm-target": "thumbv6m-none-eabi",
"panic-strategy": "abort",
Expand All @@ -26,5 +26,4 @@
"target-pointer-width": "32",
"os": "nanox",
"target-family": [ "bolos" ]

}
45 changes: 45 additions & 0 deletions scripts/link_wrap.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#!/usr/bin/env bash

set -eu

set -x

LD=${LD:-rust-lld}
# Needed because LLD gets behavior from argv[0]
LD=${LD/-ld/-lld}
${LD} "$@" --emit-relocs

echo RUST_LLD DONE

while [ $# -gt 0 -a "$1" != "-o" ];
do
shift;
done
OUT="$2"

echo OUT IS $OUT

# the relocations for the constants section are required
${OBJCOPY} --dump-section .rel.rodata=$OUT-rodata-reloc $OUT /dev/null
# there might not _be_ nonempty .data or .nvm_data sections, so there might be no relocations for it; fail gracefully.
${OBJCOPY} --dump-section .rel.data=$OUT-data-reloc $OUT /dev/null || true
${OBJCOPY} --dump-section .rel.nvm_data=$OUT-nvm-reloc $OUT /dev/null || true
# Concatenate the relocation sections; this should still write $OUT-relocs even if $OUT-data-reloc doesn't exist.
cat $OUT-rodata-reloc $OUT-nvm-reloc $OUT-data-reloc > $OUT-relocs || true
# pad the relocs out to size - we should probably make some way to adjust this size from the source.

reloc_allocated_size="$((0x$(${NM} $OUT | grep _reloc_size | cut -d' ' -f1)))"
reloc_real_size="$(stat --format %s $OUT-relocs)"
# Check that our relocations _actually_ fit.
if [ "$reloc_real_size" -gt "$reloc_allocated_size" ]
then
echo "Insufficient size for relocs; increase it in build.rs."
echo "Available size: " $reloc_allocated_size " Used size: " $reloc_real_size
exit 1
else
echo "Sufficient size:" $reloc_allocated_size $reloc_real_size
fi

truncate -s $reloc_allocated_size $OUT-relocs
# and write the relocs to their section in the flash image.
${OBJCOPY} --update-section .rel_flash=$OUT-relocs $OUT
Loading

0 comments on commit abbc6d4

Please sign in to comment.