一生一芯第五课 “程序的执行和模拟器”

感谢 yzh 大神的分享!!!!

gcc的几个选项:

-ffreestanding (表示编译出的程序将在裸机上运行)(注意:freestanding运行时环境默认的入口是_start())
Assert that compilation targets a freestanding environment. This implies -fno-builtin. A freestanding environment is one in which the standard library may not exist, and program
startup may not necessarily be at "main". The most obvious example is an OS kernel. This is equivalent to -fno-hosted.

-nostdlib (链接时不使用标准系统启动文件以及标准库)
Do not use the standard system startup files or libraries when linking. No startup files and only the libraries you specify are passed to the linker, and options specifying linkage of
the system libraries, such as -static-libgcc or -shared-libgcc, are ignored.

The compiler may generate calls to "memcmp", "memset", "memcpy" and "memmove". These entries are usually resolved by entries in libc. These entry points should be supplied through some
other mechanism when this option is specified.

One of the standard libraries bypassed by -nostdlib and -nodefaultlibs is libgcc.a, a library of internal subroutines which GCC uses to overcome shortcomings of particular machines, or
special needs for some languages.

In most cases, you need libgcc.a even when you want to avoid other standard libraries. In other words, when you specify -nostdlib or -nodefaultlibs you should usually specify -lgcc as
well. This ensures that you have no unresolved references to internal GCC library subroutines. (An example of such an internal subroutine is "__main", used to ensure C++ constructors
are called.)

-Wl,-Ttext=0x80000000 告诉链接器,把代码段的起始地址放在 0x80000000

 

在qemu运行时,可以使用 ctrl + a + c 进入monitor 模式,接着使用 info mtree 打印出 qemu 的内存布局,可以看到0x10000000是qemu的串口内存映射所在地

使用objdump时,加上 -M no-aliases 可以把所有伪指令转化成原指令

 

这里需要注意,第二个框框中,在进行编译时添加了 -O2 选项,这是很重要的。-O2选项会让编译器把所有函数展开,从而省去了把参数压栈,然后jump进入函数的步骤,因此保证了程序编译出来只有 addi 和 ebreak 指令

 

 

 

注意,llvm-objcopy -j .text -O binary prog prog.bin 的意思是,从prog二进制文件从,以二进制的格式抽取出代码段,放入prog.bin文件中

要编写可读可维护的代码哦!!!

 

 

strerror(errno) 可以打印类似 Permission denied, No such file or directory 之类的错误信息。有两个好处:

1. 不用你自己实现

2. 比你自己写得好

此外,还要尽可能拒绝 copy-paste,因为copy-paste很可能会在多处引入错误。下面是一个copy-paste引入维护

难度的例子

使用编程语言的特性还能进一步提高可读性和可维护性,如下图

 以下是总结

 ==================== Verilator: RTL 仿真简介(这一段涉及了Chisel和Verilog,以后学了Chisel,也可以回来看看,会很有帮助) =======================

(还详解了Verilator,分析了Verilator根据.v文件生成的C++仿真程序代码)

a

a

省略很多页PPT,主要跟 chisel. verilog ,verilator 有关

a

a

a

posted @ 2022-10-17 19:32  yinhuachen  阅读(171)  评论(0编辑  收藏  举报