Step 4 of Self-writing CPU (3) -- Establishment of MIPS compiling environment

Source: Internet
Author: User

I will upload my new book "Write CPU by myself" (not published yet). Today is 13th articles. I try to write them every Thursday.

4.4 create the MIPs compiling environment

The openmips processor is designed to be compatible with the mips32 instruction set architecture. Therefore, you can use the existing GNU development tool chain under the mips32 architecture. This section describes how to install and use the GNU development tool chain and how to create makefile files to compile the test program in a more convenient, fast, and automatic manner, and get the initialization file inst_rom.data of the instruction memory Rom.

4.4.1 installation and setting of visualbox

The GNU tool chain must be installed in a Linux environment. Most readers may use the Windows platform. You can first install the Linux virtual machine and then install the GNU tool chain in the Linux virtual machine. I recommend that you use a Linux virtual machine image provided on the opencores site, which is pre-installed with the Ubuntu system.

Enter the address in the browser: Ghost.


Download the latest file. I will use version 2011-12-15. Decompress the file after the download is complete, about 4 GB. In this case, you need to download visualbox to open the file. Visualbox is an open-source Virtual Machine software. This book uses version 4.1.22. After the download is complete, install visualbox. After the installation is complete, open visualbox, as shown in Figure 4-15.


Click "new" to see the "new Virtual Machine" Wizard, click "Next", the page shown in 4-16 is displayed.

 

Select Linux as the operating system, UBUNTU as the version, and click Next to set the memory size, as shown in Figure 4-17.


The memory size is set based on the computer situation. I set it to 512 MB, which is enough. After all, the program we need to compile is very simple. click Next, select "use existing virtual hard disk" and then select the unzipped Virtual Machine file, as shown in Figure 4-18.


Click "Next". visualbox lists all the settings you just set. After confirming the settings are correct, click "CREATE" to create the virtual machine. Start the VM, as shown in Figure 4-19.


Now that the Linux virtual machine has been installed, you need to do more, that is, set the sharing between the virtual machine and the Windows host machine, so that files can be transferred between the two systems. Disable the Ubuntu virtual machine, open the virtual machine setting interface in visualbox, and select "shared folder", as shown in Figure 4-20.


Click the Add folder button on the right of the page. The page shown in 4-21 is displayed:


Select the path of the shared folder and set the name, as shown in Figure 4-21. After setting, you can start the virtual machine, open the terminal, and enter the following command:

sudo mount –t vboxsf UbuntuShareFolder /mnt/

This command is used to mount the shared folder to the/mnt/directory. sudo indicates that the command is executed as a root user, and the hacker will prompt you to enter the password, by default, the password of the root user of the Ubuntu virtual machine is openrisc. In this way, the file sharing between the virtual machine and the host machine is realized. For the virtual machine, the shared file is placed under the/mnt/path, and for the host machine, the shared file is placed under the fdisk volume tusharefolder folder shown in Figure 4-21.

4.4.2 install the GNU tool chain

Copy the file to the shared folder set in the previous section to access the file through the Ubuntu virtual machine. Copy the installation file to the/OPT directory of ubuntu, open the Ubuntu terminal, and run the following command to decompress the package:

cd /opttar vfxj mips-sde-elf-i686-pc-linux-gnu.tar.tar

Open the home folder of the user's home directory, and select View> show hidden files from the menu bar to display all the files. a hidden file can be found. bashrc, add the path settings at the end of this file, as shown below.

export PATH=”$PATH:/opt/mips-4.3/bin”

Restart the Ubuntu system.

After the terminal is restarted, enter MIPS-Sde-elf-in the terminal, and press the tab twice to list all the compiled tools for the MIPs platform, as shown in Figure 4-22, indicates that the GNU toolchain is successfully installed.

The GNU tool chain contains many tools, but we don't need much. The main tools are as follows. The common name is used here. For the MIPs platform tool, the prefix "MIPS-Sde-elf-" is added before the name.

  • As: the GNU Compiler, also known as gas (GNU Compiler ER), is used to compile the Assembly source program to generate the target file.
  • Ld: GNU linker. The target files generated by as need to be linked by LD and relocated to generate executable files.
  • Objcopy: used to copy a target file in one format to another format.
  • Objdump: Used to list information about binary files.
  • Readelf: similar to objdump, but it can only process files in ELF format.

4.4.3 use GNU tool for compilation

This section uses the GNU tool to compile the test program in section 4.3. First, create a new file named inst_rom.s In the Ubuntu virtual machine. The content is as follows. Compared with the test program in section 4.3, three more Compilation instruction statements are provided.

. Org 0x0 // indicates that the program starts from the address 0x0. global _ start // defines a global symbol _ start. set noat // allows free use of registers $1 _ start: Ori $1, $1100x1100 #$1 = $0 | 0x1100 = 0 x Ori $2, $0020x0020 #$2 = $0 | 0x0020 = 0 x Ori $3, $0, 0xff00 #$3 = $0 | 0xff00 = 0xff00 Ori $4, $0, 0 xFFFF #$4 = $0 | 0 xFFFF = 0 xFFFF

This is an assembly control pseudo operation. When introducing the General registers in the mips32 architecture in Chapter 1st, we have mentioned that all General registers in the mips32 have the same name, and their usage also follows some conventions, such as: Register $1, the Convention name for programming is at, which is generally left for the assembler and not directly used in the program. If you use it directly, the assembler will issue a warning. Setting "set noat" here means you can use register $1 freely without warning.

Open the terminal in Ubuntu, use the CD command to adjust the path to the directory where inst_rom.s is located, and then use the following command to compile the code. The "-mips32" option is added to indicate compilation based on the mips32 instruction set architecture.

mips-sde-elf-as –mips32 inst_rom.S –o inst_rom.o

The above command will get the target code inst_rom.o. Open the inst_rom.o file and you can find that the first four bytes are 0x7f, 0x45, 0x4c, and 0x46. This indicates that inst_rom.o is an elf file.

For ease of understanding, the following section briefly introduces the ELF file. If you are not interested or want to learn about the compilation link process as soon as possible, you can skip the following section and read section 4.4.4.

The executable link format of ELF (executable and linkableformat) is developed and released by the UNIX System Lab (USL) as an application binary interface (ABI: Application binary interface. There are three types of ELF target files.

(1) relocatable file: stores code and appropriate data to create an executable or shared file with other object files.

(2) Executable File: stores a program for execution, which shows how to create a program process image.

(3) share the target file: contains the code and data linked in two use environments. First, the linker (LD) can process it with other relocated files and shared target files to generate another target file (for example, the compiler and the linker put *. O and *. so together into *. EXE file ). Secondly, the dynamic linker can combine it with an executable file and other shared target files to create a process image (for example, the dynamic loader adds *. EXE program and *. so is loaded into the memory for execution ).

Regardless of the type of ELF file, the structure is the same. The ELF File consists of four parts: Elf header, program header table, sections, and section header table. The first part is the elf header, which is defined as follows:

# Define ei_nident 16 typedef struct {unsigned char e_ident [ei_nident]; // occupies 16 bytes of inclue_type; // elf32_half indicates two bytes of elf32_half e_machine; elf32_word e_version; // elf32_word indicates that the value is 4 bytes e_e_entry; // bytes also indicates the size of 4 bytes elf32_off e_phoff; // elf32_off also indicates the size of 4 bytes elf32_off e_shoff; elf32_word e_flags; inclue_ehsize; elf32_half e_phentsize; elf32_half e_phnum; elf32_half e_shentsize; elf32_half e_shnum; elf32_half e_shstrndx;} elf32_ehdr;

The first four bytes are fixed: 0x7f, followed by the elf three-character ASCII code. These four bytes indicate that this file is an elf file. The following uses inst_rom.o as an example to describe the meaning of the byte following the e_ident field. See Figure 4-23.

  • The value of e_type is 01, indicating that the object can be relocated.
  • E_machine indicates the architecture required to run the program. Here it is 0x08, indicating MIPS r3000
  • E_version indicates the file version, which is 1
  • E_entry indicates the entry address of the program, which is 0x0.
  • E_phoff is the offset (in bytes) of the program header table in the file. Here it is 0x0
  • E_shoff is the offset (in bytes) of section header table in the file. Here it is 0x98
  • E_flags stores the specific processor information of the related files. 0x50000000 indicates that mips32
  • E_ehsize indicates the size of the elf header, Which is 0x34
  • E_phentsize indicates the size of each entry (one program header) in the program header table. The value is 0x0.
  • E_phnum indicates the number of entries in the program header table. The value is 0.
  • E_shensize indicates the size of each entry (a section header) in the section header table. The value is 0x28.
  • E_shnum indicates the number of entries in section header table. The value is 0x09.
  • E_shstrndx stores the partition head table index of the entry related to the orders table. The value is 0x06.

The above explanation shows that this file is a relocatable file, not an executable file, and the information about the program header table and section header table contained in this file. For inst_rom.o, there is no program header table. According to the given offset information, we can get the location of the section header table and get the location of each section through the section header table.

Of course, according to the content of the elf header and the section header table, we can analyze all sections as shown in the following figure. However, the efficiency is too slow. With the help of MIPS-Sde-elf-readelf In the GNU tool chain, we can directly obtain section information, as shown in Figure 4-24.


Note that the "-s" option is added. The information of nine sections is listed here. the starting address of the section "text" is 0x34 and the length is 0x10. We will list the content of this section, as shown in 4-25.


According to section 4.3.3, the 0x10 bytes are the binary characters corresponding to the four commands in the test program.

4.4.4 A relocable ELF file can be obtained through compiling through the link using the GNU tool, but this file cannot be executed yet. It must be converted to an executable file through the link before execution. Run the MIPs-Sde-elf-LD link tool to complete this task. In the MIPs-Sde-elf-LD parameter, you must declare a link description script, the link description script describes how each section of the input file maps to each section of the output file and controls the memory layout of sections and symbols in the output file. You can create a document as the link description script named Ram. LD. The content is as follows.
MEMORY        {               ram    : ORIGIN = 0x00000000, LENGTH = 0x00001000        }SECTIONS{.text :        {        *(.text)        } > ram        .data :        {        *(.data)        } > ram        .bss :        {        *(.bss)        } > ram}Entry(_start)

A storage block-ram is defined. Its starting address is 0x0 and its length is 0x1000. Then, it indicates that the linker output file contains three sections. text ,. data ,. BSS, where. text is stored from the starting address of RAM, followed. data ,. BSS, and enter the section of the file. text is stored in the output file. section of the input file in text. data is stored in the output file. section of the input file. BSS is stored in the output file. BSS. The last entry specifies the entry address of the program, that is, the address of the first executed command is the value of the _ start symbol. From the assembly code, we can see that the _ start symbol is 0x0. Now you can use the linker. Enter the following command in the terminal of the Ubuntu virtual machine.

mips-sde-elf-ld –T ram.ld inst_rom.o –o inst_rom.om

Get the linked file inst_rom.om, which is also an elf format file, as shown in its elf header4-26.


The previous section analyzes the elf header of inst_rom.o by hand, mainly to help readers understand it. In fact, you can use a tool to analyze the elf header directly. Enter the following command in the terminal to automatically analyze the elf header of inst_rom.om.

mips-sde-elf-readelf –h inst_rom.om
The parameter "-h" indicates that only the elf header is read. Expected result 4-27 is displayed.

Inst_rom.om is an executable file. Readers may have noticed that inst_rom.om has more program header than inst_rom.o, which is not in inst_rom.o. Like section header, the program header can also be described using a struct, as shown below.

typedef struct{       Elf32_Word          p_type;       Elf32_Off           p_offset;       Elf32_Addr          p_vaddr;       Elf32_Addr          p_paddr;       Elf32_Word          p_filez;       Elf32_Word          p_memsz;       Elf32_Word          p_flags;       Elf32_Word          p_align;}Elf32_Phdr;
We still use the tool MIPS-Sde-elf-readelf to analyze a program header from inst_rom.om, and then explain the meaning of each of the above items with this program header. Run the following command to obtain information about the program header.

mips-sde-elf-readelf –l inst_rom.om
The "-l" parameter is added to list information about the program header, as shown in Figure 4-28.

Describes the meaning of each field in the program header:

  • P_type is load, indicating Loading
  • P_offset indicates the offset of the first byte of the segment in the file inst_rom.om, Which is 0x10000.
  • P_vaddr indicates that the first byte of the segment is in the memory address, where 0
  • P_paddr is 0. In the system associated with the physical address location, this member is retained for the physical address of this segment.
  • P_filez indicates the length of the segment in the file, which is 0x10.
  • P_memsz indicates the length of the segment in the memory, which is 0x10.
  • P_flags is re, which indicates readable and executable
  • P_align is 0x10000. Based on this option, determine how segments are aligned in files and memory.

This program header indicates that the 0x10 bytes starting from the offset 0x10000 in the inst_rom.om file are placed at 0x0 in the memory, start inst_rom.om and you will find that the content of the 0x10 bytes starting from the offset 0 x is consistent with the section in inst_rom.o. the content of text is the same. Therefore, when this program section is loaded into the memory, four commands of the test program will be stored in the memory 0x10 bytes starting from 0x0.

After analysis, do readers have a better understanding of the compilation and link processes than before? In fact, this background knowledge has little to do with the openmips processor, but it helps to understand the process of compiling links. To sum up, the process of compiling the link is very simple. It only takes two steps.

Compile: MIPS-Sde-elf-as-mips32 inst_rom.s-O inst_rom.o link: MIPS-Sde-elf-LD-T Ram. LD inst_rom.o-O inst_rom.om
4.4.5 get the ROM Initialization File

The inst_rom.om obtained in the previous section is an executable file in ELF format, which is very different from the format of the expected instruction memory Rom initialization file inst_rom.data and requires format conversion. Another tool, MIPS-Sde-elf-objcopy, is provided in the GNU tool chain to convert a target file in one format to another. Here, you can use MIPS-Sde-elf-objcopy to obtain the binary form of inst_rom.om. The usage is as follows. The contents of the obtained binary file inst_rom.bin are 4-29.

mips-sde-elf-objcopy –O binary inst_rom.om inst_rom.bin

From Figure 4-29, we can see that the content of the binfile is the binary character corresponding to the four commands in the test program. Now we only need to write a small program to convert the binfile to the format of the memory initialization file in ModelSim. This small program is very simple, and no code is listed here. You can find the source program in the optical disk attached to this book, and the program name is bin2mem.exe. The usage is as follows. The obtained inst_rom.data file is 4-30.

./Bin2Mem.exe –f inst_rom.bin –o inst_rom.data

Now, let's recall that it takes four steps to get the instruction memory Rom initialization file that can be used for Modelsim simulation from the source code: compilation, Link, binfile, format conversion, as shown below.

Compile: MIPS-Sde-elf-as-mips32 inst_rom.s-O inst_rom.o link: MIPS-Sde-elf-LD-T Ram. LD inst_rom.o-O inst_rom.om obtains the binfile: MIPS-Sde-elf-objcopy-O binary inst_rom.om inst_rom.bin format conversion :. /bin2mem.exe-F inst_rom.bin-O inst_rom.data

The code for stage 1 will be uploaded later!



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.