其他分享
首页 > 其他分享> > 循序渐进学习开发RISCV-OS

循序渐进学习开发RISCV-OS

作者:互联网

exercises

ch3 编译与链接

练习3-1

使⽤ gcc 编译代码并使⽤ binutils ⼯具对⽣

成的⽬标文件和可执⾏文件(ELF 格式)进⾏分析。具体要求如下:

#include <stdio.h>

int main(void)
{
    printf("hello world!\n");
    return 0;
}
 gcc hello.c -c -o hello.o

补充gcc常用选项:

gcc [options] [filename]

常用选项含义
-E只做预处理
-c只编译不连接,生成目标文件".o"
-S生成汇编代码
-o file把输出生成到由file指定文件名的文件中
-g在输出的文件中加入支持调试的信息
-v显示输出详细的命令执行过程信息
readelf -h hell.o

信息如下:

ELF 头:
  Magic:   7f 45 4c 46 02 01 01 00 00 00 00 00 00 00 00 00 
  类别:                              ELF64
  数据:                              2 补码,小端序 (little endian)
  Version:                           1 (current)
  OS/ABI:                            UNIX - System V
  ABI 版本:                          0
  类型:                              REL (可重定位文件)
  系统架构:                          Advanced Micro Devices X86-64
  版本:                              0x1
  入口点地址:               0x0
  程序头起点:          0 (bytes into file)
  Start of section headers:          792 (bytes into file)
  标志:             0x0
  Size of this header:               64 (bytes)
  Size of program headers:           0 (bytes)
  Number of program headers:         0
  Size of section headers:           64 (bytes)
  Number of section headers:         14
  Section header string table index: 13
readelf -SW hello.c

信息如下:

 There are 14 section headers, starting at offset 0x318:
 
  节头:
    [Nr] Name              Type            Address          Off    Size   ES Flg Lk Inf Al
    [ 0]                   NULL            0000000000000000 000000 000000 00      0   0  0
    [ 1] .text             PROGBITS        0000000000000000 000040 00001b 00  AX  0   0  1
    [ 2] .rela.text        RELA            0000000000000000 000258 000030 18   I 11   1  8
    [ 3] .data             PROGBITS        0000000000000000 00005b 000000 00  WA  0   0  1
    [ 4] .bss              NOBITS          0000000000000000 00005b 000000 00  WA  0   0  1
    [ 5] .rodata           PROGBITS        0000000000000000 00005b 00000d 00   A  0   0  1
    [ 6] .comment          PROGBITS        0000000000000000 000068 00002b 01  MS  0   0  1
    [ 7] .note.GNU-stack   PROGBITS        0000000000000000 000093 000000 00      0   0  1
    [ 8] .note.gnu.property NOTE            0000000000000000 000098 000020 00   A  0   0  8
    [ 9] .eh_frame         PROGBITS        0000000000000000 0000b8 000038 00   A  0   0  8
    [10] .rela.eh_frame    RELA            0000000000000000 000288 000018 18   I 11   9  8
    [11] .symtab           SYMTAB          0000000000000000 0000f0 000138 18     12  10  8
    [12] .strtab           STRTAB          0000000000000000 000228 000029 00      0   0  1
    [13] .shstrtab         STRTAB          0000000000000000 0002a0 000074 00      0   0  1
  Key to Flags:
    W (write), A (alloc), X (execute), M (merge), S (strings), I (info),
    L (link order), O (extra OS processing required), G (group), T (TLS),
    C (compressed), x (unknown), o (OS specific), E (exclude),
    l (large), p (processor specific)
objdump -S hello.o

反汇编:

hello.o:     文件格式 elf64-x86-64


Disassembly of section .text:

0000000000000000 <main>:
#include <stdio.h>

int main(void)
{
   0:   f3 0f 1e fa             endbr64 
   4:   55                      push   %rbp
   5:   48 89 e5                mov    %rsp,%rbp
    printf("hello world!\n");
   8:   48 8d 3d 00 00 00 00    lea    0x0(%rip),%rdi        # f <main+0xf>
   f:   e8 00 00 00 00          callq  14 <main+0x14>
    return 0;
  14:   b8 00 00 00 00          mov    $0x0,%eax
  19:   5d                      pop    %rbp
  1a:   c3                      retq   

练习3-2

如下例⼦ C 语⾔代码:

#include <stdio.h>
int global_init = 0x11111111;
const int global_const = 0x22222222;
void main()
{
    static int static_var = 0x33333333;
    static int static_var_uninit;
    int auto_var = 0x44444444;
    printf("hello world!\n");
    return;
}

请问编译为 .o 文件后,global_init, global_const, static_var, static_var_uninit, auto_var 这些变 量分别存放在那些 section ⾥,“hello world!\n” 这个字符串⼜在哪⾥?并尝试⽤⼯具查看并验证你的猜测。

ch4 嵌入式开发介绍

练习4-1

熟悉交叉编译概念,使⽤ riscv gcc 编译代码并使⽤ binutils ⼯具对⽣成的⽬标文件和可执⾏文件(ELF 格式) 进⾏分析。具体要求如下:

#include <stdio.h>

int main(void)
{
    printf("hello world!\n");
    return 0;
}

riscv64-unknown-elf-gcc -march=rv32ima -mabi=ilp32 -c hello.c
readelf -h hello.o

信息如下:

ELF 头:
  Magic:   7f 45 4c 46 01 01 01 00 00 00 00 00 00 00 00 00 
  类别:                              ELF32
  数据:                              2 补码,小端序 (little endian)
  Version:                           1 (current)
  OS/ABI:                            UNIX - System V
  ABI 版本:                          0
  类型:                              REL (可重定位文件)
  系统架构:                          RISC-V
  版本:                              0x1
  入口点地址:               0x0
  程序头起点:          0 (bytes into file)
  Start of section headers:          556 (bytes into file)
  标志:             0x0
  Size of this header:               52 (bytes)
  Size of program headers:           0 (bytes)
  Number of program headers:         0
  Size of section headers:           40 (bytes)
  Number of section headers:         11
  Section header string table index: 10
readelf -S hello.o

信息如下:

There are 11 section headers, starting at offset 0x22c:

节头:
  [Nr] Name              Type            Addr     Off    Size   ES Flg Lk Inf Al
  [ 0]                   NULL            00000000 000000 000000 00      0   0  0
  [ 1] .text             PROGBITS        00000000 000034 000038 00  AX  0   0  4
  [ 2] .rela.text        RELA            00000000 000190 000048 0c   I  8   1  4
  [ 3] .data             PROGBITS        00000000 00006c 000000 00  WA  0   0  1
  [ 4] .bss              NOBITS          00000000 00006c 000000 00  WA  0   0  1
  [ 5] .rodata           PROGBITS        00000000 00006c 00000d 00   A  0   0  4
  [ 6] .comment          PROGBITS        00000000 000079 000029 01  MS  0   0  1
  [ 7] .riscv.attributes RISCV_ATTRIBUTE 00000000 0000a2 000026 00      0   0  1
  [ 8] .symtab           SYMTAB          00000000 0000c8 0000b0 10      9   9  4
  [ 9] .strtab           STRTAB          00000000 000178 000018 00      0   0  1
  [10] .shstrtab         STRTAB          00000000 0001d8 000054 00      0   0  1
Key to Flags:
  W (write), A (alloc), X (execute), M (merge), S (strings), I (info),
  L (link order), O (extra OS processing required), G (group), T (TLS),
  C (compressed), x (unknown), o (OS specific), E (exclude),
  p (processor specific)

练习4-2

基于 练习 4-1 继续熟悉 qemu/gdb 等⼯具的使⽤,具体要求如下:

riscv64-unknown-elf-gcc -g -march=rv32ima -mabi=ilp32  hello.c
qemu-riscv32 ./a.out

可以看到控制台输出以下内容:

hello world!

标签:00,RISCV,00000000,0000000000000000,headers,循序渐进,section,OS,hello
来源: https://blog.csdn.net/qq_42913442/article/details/122850401