{"id":30,"date":"2019-11-12T12:27:00","date_gmt":"2019-11-12T17:27:00","guid":{"rendered":"https:\/\/www.vociferousvoid.org\/?p=30"},"modified":"2023-07-01T22:04:10","modified_gmt":"2023-07-02T02:04:10","slug":"risc-v-bare-metal-programming-chapter-3-a-link-to-the-past","status":"publish","type":"post","link":"https:\/\/www.vociferousvoid.org\/index.php\/2019\/11\/12\/risc-v-bare-metal-programming-chapter-3-a-link-to-the-past\/","title":{"rendered":"RISC-V Bare Metal Programming Chapter 3: A Link to the Past"},"content":{"rendered":"\n<p>Previous chapters of the RISC-V bare metal programming tutorial have focused primarily on the assembler. In <a href=\"https:\/\/www.vociferousvoid.org\/index.php\/2019\/11\/06\/risc-v-bare-metal-programming-chapter-2-opcodes-assemble\/\">chapter 2<\/a>, assembler directives were discussed along with their relationship to the positioning of code in an executable. The various sections of where code and data reside have well defined semantics in the Executable and Linkable Format specification. In this chapter, these semantics and the linking process will be examined in more detail.<\/p>\n\n\n\n<p>The typical programming workflow involves processing the source file using either an assembler (for assembly code), or a compiler (for higher-level programming languages such as C) to produce an object file. The object file by itself cannot be run since it will have references to memory addresses which are relative to the code&#8217;s position rather than absolute memory offsets. The relative addresses need to be translated into absolute addresses in order for them to make sense to the processor. For example, the following code to call the <code>sum<\/code> function:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>        .text\n        .align 2\n        .global _start\n        .global _stack_end\n_start:\n        li      a0, 5\n        li      a1, 4\n        la      sp,_stack_end\n        call    sum\nstop:   j       stop<\/code><\/pre>\n\n\n\n<p>Will be assembled to the following object code:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code> 1: $ riscv64-unknown-elf-objdump -d main.o\n 2: \n 3: main.o:     file format elf64-littleriscv\n 4: \n 5: \n 6: Disassembly of section .text:\n 7: \n 8: 0000000000000000 &lt;_start&gt;:\n 9:    0:\t00500513          \tli\ta0,5\n10:    4:\t00400593          \tli\ta1,4\n11:    8:\t00000117          \tauipc\tsp,0x0\n12:    c:\t00010113          \tmv\tsp,sp\n13:   10:\t00000097          \tauipc\tra,0x0\n14:   14:\t000080e7          \tjalr\tra # 10 &lt;_start+0x10&gt;\n15: \n16: 0000000000000018 &lt;stop&gt;:\n17:   18:\t0000006f          \tj\t18 &lt;stop&gt;\n18: <\/code><\/pre>\n\n\n\n<p>The <code>auipc<\/code> (Add Upper Immediate to PC) adds 0 to the program counter on line <a href=\"https:\/\/www.vociferousvoid.org\/main\/riscv_bare_metal_chapter3#coderef-set_stack\">11<\/a> which stores the value of the program counter in the <code>sp<\/code> register. This is followed by a no-op (a move of the value of <code>sp<\/code> into <code>sp<\/code> which simply increments the program counter). The purpose of this sequence of instructions is to set the top of the stack, the zero in this instruction must be replaced with the stack&#8217;s address. The linker is responsible for filling in the correct address which is at the offset defined by the <code>_stack_end<\/code> symbol. The final linked application is shown in the following listing:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ riscv64-unknown-elf-objdump -d sum.elf\n\nsum.elf:     file format elf64-littleriscv\n\n\nDisassembly of section .text:\n\n0000000080000000 &lt;_start&gt;:\n    80000000:\t00500513          \tli\ta0,5\n    80000004:\t00400593          \tli\ta1,4\n    80000008:\t00009117          \tauipc\tsp,0x9\n    8000000c:\tff810113          \taddi\tsp,sp,-8 # 80009000 &lt;_stack_end&gt;\n    80000010:\t008000ef          \tjal\tra,80000018 &lt;sum&gt;\n\n0000000080000014 &lt;stop&gt;:\n    80000014:\t0000006f          \tj\t80000014 &lt;stop&gt;\n\n0000000080000018 &lt;sum&gt;:\n    80000018:\tfe010113          \taddi\tsp,sp,-32\n    8000001c:\t00113c23          \tsd\tra,24(sp)\n    80000020:\t00b50533          \tadd\ta0,a0,a1\n    80000024:\t01813083          \tld\tra,24(sp)\n    80000028:\t02010113          \taddi\tsp,sp,32\n    8000002c:\t00008067          \tret<\/code><\/pre>\n\n\n\n<p>In this listing, the <code>auipc<\/code> instruction will set <code>sp<\/code> to address <code>0x80009008<\/code> because:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>pc<\/code> is <code>0x80000008<\/code> (the offset of the current instruction)<\/li>\n\n\n\n<li>The result of <code>auipc<\/code> is <code>sp = pc + (0x9 &lt;&lt; 12)<\/code><\/li>\n<\/ul>\n\n\n\n<p>The next instruction will subtract 8 from the value of <code>sp<\/code> to set the top of the stack at memory offset <code>0x80009000<\/code>.<\/p>\n\n\n\n<p>Similarly, the next two instructions starting at line <a href=\"https:\/\/www.vociferousvoid.org\/main\/riscv_bare_metal_chapter3#coderef-call_sum\">13<\/a> (offsets <code>0x10<\/code> and <code>0x14<\/code>) of the object file are place-holders for the the call to the <code>sum<\/code> subroutine. The <code>auipc<\/code> instruction sets the <code>ra<\/code> register to the current value of the program counter (<code>ra = pc + (0x0 &lt;&lt; 12)<\/code>), the next instruction jumps to the address in <code>ra<\/code> (offset <code>10<\/code>) then sets <code>ra<\/code> to <code>pc + 4<\/code> (<code>0x18<\/code>). If the program could execute as-is, this would result in an infinite loop. The proper memory offsets need to be filled in by the linker.<\/p>\n\n\n\n<p>In the final linked program, these two instructions are replaced by a <code>jal<\/code> which sets <code>ra<\/code> (the return address) to the instruction at <code>0x80000014<\/code> (the infinite loop), then jumps to the offset at <code>0x80000018<\/code> (the start of the <code>sum<\/code> subroutine).<\/p>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"orgaab4bce\">The Adventure of Link<\/h1>\n\n\n\n<p>Although the linking phase may seem like magic, it is largely under the control of the developer via the linker script. Chapter 2 introduced an example linker script. However, the explanation of its purpose was very superficial. In this chapter, the process of linking the application will be studied more thoroughly. The linker script from the chapter 2 is illustrated in the following listing:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code> 1: OUTPUT_ARCH( \"riscv\" )\n 2: SECTIONS {\n 3: \t. = 0x80000000;\n 4: \t.text : {\n 5: \t\tPROVIDE(_text_start = .);\n 6: \t\t* (.text.init);\n 7: \t\t* (.text .text.*);\n 8: \t\tPROVIDE(_text_end = .);\n 9: \t}\n10: \tPROVIDE(_global_pointer = .);\n11: \t.rodata : {\n12: \t\tPROVIDE(_rodata_start = .);\n13: \t\t*(.srodata .srodata.*) *(.rodata .rodata.*)\n14: \t\tPROVIDE(_rodata_end = .);\n15: \t}\n16: \t.data : {\n17: \t\t. = ALIGN(4096);\n18: \t\tPROVIDE(_data_start = .);\n19: \t\t*(.sdata .sdata.*) *(.data .data.*)\n20: \t\tPROVIDE(_data_end = .);\n21: \t}\n22: \t.bss : {\n23: \t\tPROVIDE(_bss_start = .);\n24: \t\t*(.sbss .sbss.*) *(.bss .bss.*)\n25: \t\tPROVIDE(_bss_end = .);\n26: \t}\n27: \tPROVIDE(_stack_start = _bss_end);\n28: \tPROVIDE(_stack_end = _stack_start + 0x8000);\n29: }<\/code><\/pre>\n\n\n\n<p>The first statement in the linker script sets the architecture of the target machine; in this case RISC-V.<\/p>\n\n\n\n<p>The more important command is the <code>SECTIONS<\/code> statement which defines the different sections of the ELF file. As discussed in chapter 2, code and data have different memory requirements. Typically code will be stored in read-only memory and data will be stored in memory that can be read-only or writable depending on the constraints on the data. The <code>SECTIONS<\/code> declaration is used to prescribe how the code and data will be organized in the final binary.<\/p>\n\n\n\n<p>Within the <code>SECTIONS<\/code> block of the script, the period (.) is a special token that represents the location counter. This is essentially the current offset in memory. By default the location counter always starts at offset 0. However, the current position can be set explicitly by assigning to the period token. Since the reset vector of the VirtIO board is at memory address <code>0x80000000<\/code>, this address is assigned to the location counter to ensure that this area of memory is populated by the linked binary.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"org4516f2c\">Code<\/h2>\n\n\n\n<p>Once the location counter is initialized, the next statement in the linker script is the declaration of the .text section. This is where all the executable code is expected to be found. The section is declared by specifying its name (<code>.text<\/code>) followed by a colon (:) and a pair of braces that enclose the statements specific to the current section.<\/p>\n\n\n\n<p>The first statement in the <code>.text<\/code> section block is a <code>PROVIDE<\/code> command. This defines a linker symbol that can be used when linking. In this case it defines a global symbol named <code>_text_start<\/code><strong> <\/strong>which has the current value of the location counter. This symbol can be used to refer to the starting offset of the .text section in memory.<\/p>\n\n\n\n<p>The next statement uses wildcards to aggregate the assembly code in the <code>.text.init<\/code> section of all object files provided to the linker. The next statement aggregates the code from the <code>.text<\/code> and <code>.text.*<\/code> sections in all of the provided object files. The later rule will match any section name prefixed with the <code>.text.<\/code> substring.<\/p>\n\n\n\n<p>Note that the order in which object files are provided to the linker matters. If the <kbd>add.o<\/kbd> and <kbd>main.o<\/kbd> files are linked with the add function provided first, the resulting binary will have the following layout:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ riscv64-unknown-elf-ld -T linker.lds -o sum.elf add.o main.o\n$ riscv64-unknown-elf-objdump -d sum.elf \n\nsum.elf:     file format elf64-littleriscv\n\n\nDisassembly of section .text:\n\n0000000080000000 &lt;sum&gt;:\n    80000000:\tfe010113          \taddi\tsp,sp,-32\n    80000004:\t00113c23          \tsd\tra,24(sp)\n    80000008:\t00b50533          \tadd\ta0,a0,a1\n    8000000c:\t01813083          \tld\tra,24(sp)\n    80000010:\t02010113          \taddi\tsp,sp,32\n    80000014:\t00008067          \tret\n\n0000000080000018 &lt;_start&gt;:\n    80000018:\t00500513          \tli\ta0,5\n    8000001c:\t00400593          \tli\ta1,4\n    80000020:\t00009117          \tauipc\tsp,0x9\n    80000024:\tfe010113          \taddi\tsp,sp,-32 # 80009000 &lt;_stack_end&gt;\n    80000028:\tfd9ff0ef          \tjal\tra,80000000 &lt;sum&gt;\n\n000000008000002c &lt;stop&gt;:\n    8000002c:\t0000006f          \tj\t8000002c &lt;stop&gt;<\/code><\/pre>\n\n\n\n<p>This is clearly not the desired result since the first instruction will create a stack frame, save the return address (which is undefined), then perform the add with un-initialized argument registers. To ensure that the initialization comes before the function implementation, the <kbd>main.s<\/kbd> file must be changed to add its code to the <code>.text.init<\/code> section:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code> 1:         .section \".text.init\"\n 2:         .align 2\n 3:         .global _start\n 4:         .global _stack_end\n 5: _start:\n 6:         li      a0, 5\n 7:         li      a1, 4\n 8:         la      sp,_stack_end\n 9:         call    sum\n10: stop:   j       stop<\/code><\/pre>\n\n\n\n<p>The <code>main.s<\/code> file uses the <code>.section<\/code> assembler directive on line <a href=\"https:\/\/www.vociferousvoid.org\/main\/riscv_bare_metal_chapter3#coderef-init_section\">1<\/a> to declare that all code that follows should be copied into the <code>.text.init<\/code> section. This will ensure that the initialization code will always be first due to the command to aggregate code from this section preceding any other in the linker script. The linked program will now have the proper layout regardless of the order in which the object files are provided to the linker.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ riscv64-unknown-elf-ld -T linker.lds -o sum.elf add.o main.o\n$ riscv64-unknown-elf-objdump -d sum.elf \n\nsum.elf:     file format elf64-littleriscv\n\n\nDisassembly of section .text:\n\n0000000080000000 &lt;_start&gt;:\n    80000000:\t00500513          \tli\ta0,5\n    80000004:\t00400593          \tli\ta1,4\n    80000008:\t00009117          \tauipc\tsp,0x9\n    8000000c:\tff810113          \taddi\tsp,sp,-8 # 80009000 &lt;_stack_end&gt;\n    80000010:\t008000ef          \tjal\tra,80000018 &lt;sum&gt;\n\n0000000080000014 &lt;stop&gt;:\n    80000014:\t0000006f          \tj\t80000014 &lt;stop&gt;\n\n0000000080000018 &lt;sum&gt;:\n    80000018:\tfe010113          \taddi\tsp,sp,-32\n    8000001c:\t00113c23          \tsd\tra,24(sp)\n    80000020:\t00b50533          \tadd\ta0,a0,a1\n    80000024:\t01813083          \tld\tra,24(sp)\n    80000028:\t02010113          \taddi\tsp,sp,32\n    8000002c:\t00008067          \tret<\/code><\/pre>\n\n\n\n<p>The <code>_start<\/code> code is located at offset <code>0x80000000<\/code> as expected even if the object file for the <code>sum<\/code> function was provided to the linker first. Now that the code is properly organized, let&#8217;s look at the data.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"org1578180\">Data<\/h2>\n\n\n\n<p>The linker script defines three data sections: <code>.rodata<\/code>, <code>.data<\/code>, and <code>.bss<\/code>. It may seem odd to have four sections (if the <code>.text<\/code> section is counted) when a program is comprised only of two types on information: code and data. The reason is that data can be divided up into three categories:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Global Read-only Data<\/li>\n\n\n\n<li>Global Initialized Data<\/li>\n\n\n\n<li>Global Un-initialized Data<\/li>\n<\/ul>\n\n\n\n<p>Local data is not considered because this type of data is generated at run time and will be stored either in stack memory, or some allocated heap buffer. For now the focus will be on global data. The differences in each type of data can be illustrated using a simple C program:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code> 1: const int operand1 = 4 ;\n 2: int operand2 = 5 ;\n 3: int result ;\n 4: \n 5: int\n 6: sum( int op1, int op2 )\n 7: {\n 8:         return op1 + op2 ;\n 9: }\n10: \n11: int\n12: main( int argc, char** argv )\n13: {\n14:         result = sum( operand1, operand2 ) ;\n15:         while ( 1 ) ;\n16: }<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"org600643c\">Global Read-only Data<\/h3>\n\n\n\n<p>The <code>operand1<\/code> variable is declared on line <a href=\"https:\/\/www.vociferousvoid.org\/main\/riscv_bare_metal_chapter3#coderef-rodata\">1<\/a>. This is a read-only value initialized to the integer 4. The compiler will store this data in the <code>.srodata<\/code> section of the object file:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ riscv64-unknown-elf-objdump -s -j.srodata addc.o \n\naddc.o:     file format elf64-littleriscv\n\nContents of section .srodata:\n 0000 04000000                             ....<\/code><\/pre>\n\n\n\n<p>Assembler directives can also be used to populate the <code>.rodata<\/code> section in an assembly program. The following fragment can be added to the end of the <kbd>main.s<\/kbd> program to declare the <code>operand1<\/code> constant in the <code>.rodata<\/code> section:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>1:         .section \".rodata\"\n2: operand1:       .word   4<\/code><\/pre>\n\n\n\n<p>The constant <code>operand1<\/code> is defined on line <a href=\"https:\/\/www.vociferousvoid.org\/main\/riscv_bare_metal_chapter3#coderef-operand1_def\">2<\/a> using the <code>.word<\/code> assembler directive. This will store the given value as a 32-bit quantity in the current memory word. This directive allows any number of words to be stored by specifying the values as a comma separated list. For example, the following directive will store the 32-bit values <code>0x0001<\/code>, <code>0x0002<\/code>, and <code>0x0003<\/code> in successive memory words:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>.word 1, 2, 3<\/code><\/pre>\n\n\n\n<p>If this program is linked using the example linker script, this data will be included in the <code>.rodata<\/code> section. The script instructs the linker to aggregate all definitions of <code>.srodata<\/code> and <code>.rodata<\/code> as well as section names prefixed with <code>.rodata.<\/code> or <code>.srodata.<\/code> into a single <code>.rodata<\/code> section.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"org624b212\">Global Initialized Data<\/h3>\n\n\n\n<p>The <code>operand2<\/code> variable is declared on line <a href=\"https:\/\/www.vociferousvoid.org\/main\/riscv_bare_metal_chapter3#coderef-data\">2<\/a>. This is a mutable variable initialized with the integer value 5. The compiler will store this data in the <code>.sdata<\/code> section of the object file.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ riscv64-unknown-elf-objdump -s -j.sdata addc.o \n\naddc.o:     file format elf64-littleriscv\n\nContents of section .sdata:\n 0000 05000000                             ....<\/code><\/pre>\n\n\n\n<p>The <kbd>linker.lds<\/kbd> script instructs the linker to aggregate all data declared in the <code>.sdata<\/code>, and <code>.data<\/code> sections, as well as all sections prefixed by <code>.data.<\/code> or <code>.sdata.<\/code> into a single <code>.data<\/code> section.<\/p>\n\n\n\n<p>The <code>operand2<\/code> global value can similarly be defined using assembler directives. The relevant assembly code is illustrated in the following listing:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>        .data\noperand2:       .word   5<\/code><\/pre>\n\n\n\n<p>The <code>.data<\/code> assembler directive ensures that all following instructions or directives will affect the <code>.data<\/code> section.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"orga0973e2\">Global Un-initialized Data<\/h4>\n\n\n\n<p>The <code>result<\/code> variable is declared on line <a href=\"https:\/\/www.vociferousvoid.org\/main\/riscv_bare_metal_chapter3#coderef-bss\">3<\/a> of the C program. This variable is said to be un-initialized because it is not assigned a value at build time. The C language guarantees that un-initialized global variables will be initialized to zero. The system is responsible for initializing this data to zero before handing control over to the C program. This is easier to do if un-initialized data is aggregated into a common section. The loader can then simply zero out the entire section; in this case the <code>.bss<\/code> section:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ riscv64-unknown-elf-objdump -D -j.bss sumc.o \n\nsumc.o:     file format elf64-littleriscv\n\n\nDisassembly of section .bss:\n\n0000000000000000 &lt;result&gt;:\n   0:\t0000                \tunimp\n\t...<\/code><\/pre>\n\n\n\n<p>The linker script will instruct the linker to aggregate all data definitions in the <code>.sbss<\/code> or <code>.bss<\/code> sections as, well as those in section names prefixed by <code>.sbss.<\/code> or <code>.bss.<\/code>, into a single <code>.bss<\/code> section.<\/p>\n\n\n\n<p>The <code>result<\/code> global variable can also be defined using assembler directives. The relevant assembly code is illustrated in the following listing:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>        .bss\nresult:         .word   0<\/code><\/pre>\n\n\n\n<p>The <code>.bss<\/code> assembler directive will ensure that the result memory location is located in the <code>.bss<\/code> section of the binary file. Note that the value of the <code>result<\/code> variable is initialized to zero. Even though this is un-initialized data, if no value is specified, no memory will be allocated to the result variable. Since this is the <code>.bss<\/code> section, initializing this memory to zero satisfies the C language guarantees.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"org33398eb\">The Final Boss<\/h2>\n\n\n\n<p>The assembly program to add two integer values can be update to read its operands from memory. The following listing shows the updated <code>main.s<\/code> assembly code:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code> 1:         .section \".text.init\"\n 2:         .align 2\n 3:         .global _start\n 4:         .global _stack_end\n 5: _start:\n 6:         lw      a0, operand2\n 7:         lw      a1, operand1\n 8:         la      sp,_stack_end\n 9:         call    sum\n10:         la      t1, result\n11:         sw      a0, 0(t1)\n12: stop:   j       stop\n13:         .section \".rodata\"\n14: operand1:       .word   4\n15:         .data\n16: operand2:       .word   5\n17:         .bss\n18: result:         .word   0<\/code><\/pre>\n\n\n\n<p>The value of <code>operand2<\/code> is loaded into the function argument register <code>a0<\/code> on line <a href=\"https:\/\/www.vociferousvoid.org\/main\/riscv_bare_metal_chapter3#coderef-load_operand2\">6<\/a>. The value of <code>operand1<\/code> is loaded into the function argument register <code>a1<\/code> on line <a href=\"https:\/\/www.vociferousvoid.org\/main\/riscv_bare_metal_chapter3#coderef-load_operand1\">7<\/a>. The <code>sum<\/code> function is called with those operands and the result (stored in <code>a0<\/code>), is saved in the memory word allocated for <code>result<\/code> in the <code>.bss<\/code> section on line <a href=\"https:\/\/www.vociferousvoid.org\/main\/riscv_bare_metal_chapter3#coderef-save_result\">11<\/a>. Before saving the result, the memory location of the <code>result<\/code> variable must first be loaded into a register; this is accomplished using the <code>la<\/code> instruction on line <a href=\"https:\/\/www.vociferousvoid.org\/main\/riscv_bare_metal_chapter3#coderef-load_result\">10<\/a>.<\/p>\n\n\n\n<p>This program can be assembled and linked with the <code>add.s<\/code> program as follows:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ riscv64-unknown-elf-as -o main.o main.s\n$ riscv64-unknown-elf-as -o add.o add.s\n$ riscv64-unknown-elf-ld -T linker.lds -o sum.elf add.o main.o<\/code><\/pre>\n\n\n\n<p>The memory offset of the <code>result<\/code> can be obtained by inspecting the resulting ELF file:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ riscv64-unknown-elf-objdump -D -j.bss sum.elf \n\nsum.elf:     file format elf64-littleriscv\n\n\nDisassembly of section .bss:\n\n0000000080001004 &lt;result&gt;:\n    80001004:\t0000                \tunimp\n\t...<\/code><\/pre>\n\n\n\n<p>The program can now be tested using the QEMU emulator. Given that the memory location of the <code>result<\/code> variable is <code>0x80001004<\/code>, we can inspect this memory location to ensure that it contains the expected result:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ qemu-system-riscv64 -M virt -serial \/dev\/null -nographic -kernel sum.elf\nQEMU 3.1.0 monitor - type 'help' for more information\n(qemu) xp \/1dw 0x80001004\n0000000080001004:          9\n(qemu) <\/code><\/pre>\n\n\n\n<p>The memory location that corresponds with the <code>result<\/code> variable does in fact contain the value 9 (which is the sum of 4 and 5). Therefore the program is behaving as expected.<\/p>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"org944c620\">Conclusion<\/h1>\n\n\n\n<p>In this chapter, the linking process was studied in more detail. The process of linking the object files into a final binary was hopefully demystified by describing the process of transforming relative offsets to absolute memory offsets. The primary example was setting the location of the top of the stack and setting up the <code>sum<\/code> function call.<\/p>\n\n\n\n<p>The use of a linker script illustrates how code and data can more intelligently be organized in a binary file. The greater flexibility offered when using the linker script has allowed for enhancements to the original <code>add.s<\/code> program from chapter 1. The operands for the sum are now read from offsets in memory; each in different data sections. Moreover additional assembler directives were introduced in this chapter to allow better control over how code and data are placed in the linked binary file. In particular:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>.text<\/code>: Specify that what follows goes into the .text section.<\/li>\n\n\n\n<li><code>.data<\/code>: Specify that what follows goes into the .data section.<\/li>\n\n\n\n<li><code>.bss<\/code>: Specify that what follows goes into the .bss section. <\/li>\n\n\n\n<li><code>.section<\/code>: Set the section name explicitly for the code and data that follows. <\/li>\n\n\n\n<li><code>.word<\/code>: Store the specified 32-bit quantities into successive memory words.<\/li>\n<\/ul>\n\n\n\n<p>Other useful assembler directives that were not covered in this chapter include:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>.byte<\/code>: Store the specified 8-bit quantities into successive bytes of memory. <\/li>\n\n\n\n<li><code>.half<\/code>: Store the specified 16-bit quantities into successive memory half-words. <\/li>\n\n\n\n<li><code>.dword<\/code>: Store the specified 64-bit quantities into successive memory double-words. <\/li>\n\n\n\n<li><code>.string<\/code>: Store a string in memory and null-terminate it.<\/li>\n<\/ul>\n\n\n\n<p>Following chapters will start to look at some of the standard extensions of the RISC-V ISA. So far only the RV64I instructions have been used, wereas the ISA defines extensions for multiplication (RVM), atomic operations (RVA), floating point and double precision floating point operations (RVF and RVD), and compressed instructions (RVC). These extensions will be useful for building more complex programs.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Previous chapters of the RISC-V bare metal programming tutorial have focused primarily on the assembler. In chapter 2, assembler directives were discussed along with their relationship to the positioning of code in an executable. The various sections of where code and data reside have well defined semantics in the Executable and Linkable Format specification. In [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[4],"tags":[],"class_list":["post-30","post","type-post","status-publish","format-standard","hentry","category-risc-v"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.vociferousvoid.org\/index.php\/wp-json\/wp\/v2\/posts\/30","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.vociferousvoid.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.vociferousvoid.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.vociferousvoid.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.vociferousvoid.org\/index.php\/wp-json\/wp\/v2\/comments?post=30"}],"version-history":[{"count":2,"href":"https:\/\/www.vociferousvoid.org\/index.php\/wp-json\/wp\/v2\/posts\/30\/revisions"}],"predecessor-version":[{"id":32,"href":"https:\/\/www.vociferousvoid.org\/index.php\/wp-json\/wp\/v2\/posts\/30\/revisions\/32"}],"wp:attachment":[{"href":"https:\/\/www.vociferousvoid.org\/index.php\/wp-json\/wp\/v2\/media?parent=30"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.vociferousvoid.org\/index.php\/wp-json\/wp\/v2\/categories?post=30"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.vociferousvoid.org\/index.php\/wp-json\/wp\/v2\/tags?post=30"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}