Rust application development in the OpenWrt router system of the MIPS platform, openwrtrust

Source: Internet
Author: User
Tags ssh secure shell

Rust application development in the OpenWrt router system of the MIPS platform, openwrtrust

Author: Liigo (Zhuang Xiaoli)

Date: January 1, September 17, 2014

Original: http://blog.csdn.net/liigo/article/details/39347541

Copyright, reproduced please indicate the source: http://blog.csdn.net/liigo


Target

Use the Rust language to develop application software on the MIPS (el) + OpenWrt router platform.


Compile rustc

First, compile the source code of the Rust compiler and generate the cross compiler rustc that supports the mipsel-linux platform.

./configure --target=mipsel-linux && make && make install

Note that during the compilation process, the MIPS (el) + OpenWrt platform development kit SDK will be called, specifically, mipsel-linux-gcc and mipsel-linux-ar. However, the tool naming format in the SDK is mipsel-openwrt-linux-uclibc-gcc/ar, which is different from that in the Rust compiling script. A simple method is to create a symbolic link file:

cd /path/to/openwrt/sdk/toolchainln -s mipsel-openwrt-linux-uclibc-gcc mipsel-linux-gccln -s mipsel-openwrt-linux-uclibc-ar mipsel-linux-ar


Use the Rust standard library std

Compile the source file histd. rs:

fn main() {println!("Hi Rust! (uses std crate)");}
Compile histd:

rustc --target=mipsel-linux -C linker=mipsel-linux-gcc histd.rs

The executable file histd under the target platform will be generated. The file size is 1,389,884 bytes, about 1.32 MB, which is quite large! Vro devices are almost unacceptable.

This is an executable file generated by static compilation without additional Runtime Library dependencies. Strictly speaking, it only depends on libc. so in the target system. Of course, if you do not need to output text to the console, you do not need libc. so.


Use the Rust core library (without the standard library std)

Compile the source file hicore. rs:

#![no_std]#![feature(lang_items)]extern crate libc;extern crate core;use libc::puts;use core::intrinsics::transmute;#[start]fn start(_argc: int, _argv: *const *const u8) -> int {let s = "Hi Rust! (uses core crate)\0"; // &str    unsafe {    let (s,_): (*const i8, uint) = transmute(s); // see core::raw::Slice    puts(s);    }return 0;}#[lang = "stack_exhausted"] extern fn stack_exhausted() {}#[lang = "eh_personality"] extern fn eh_personality() {}#[lang = "begin_unwind"]extern fn begin_unwind(_args: &core::fmt::Arguments,                        _file: &str,                        _line: uint) -> ! {    loop {}}

Compile hicore. rs:

rustc --target=mipsel-linux -C linker=mipsel-linux-gcc hicore.rs

The executable file hicore under the target platform will be generated. The file size is only 6556 bytes, which is quite small!

This is an executable file generated by static compilation without additional Runtime Library dependencies. Strictly speaking, it only depends on libc. so in the target system. Of course, if you do not need to output text to the console, you do not need libc. so.


No standard library std, no core library core, pure streaking

Compile the source code hi. rs:

#![no_std]#![feature(lang_items)]#![feature(intrinsics)]#[link(name = "c")]extern {fn puts(s: *const u8);}#[start]fn start(_argc: int, _argv: *const *const u8) -> int {let s = "Hi Rust!\0"; // &str    unsafe {    let (s,_): (*const u8, uint) = transmute(s); // see core::raw::Slice    puts(s);    }return 0;}#[lang = "stack_exhausted"] extern fn stack_exhausted() {}#[lang = "eh_personality"] extern fn eh_personality() {}extern "rust-intrinsic" {    fn transmute<T, U>(x: T) -> U;}

Compile hi. rs:

rustc --target=mipsel-linux -C linker=mipsel-linux-gcc hi.rs
The executable file hi under the target platform will be generated. The file size is only 6504 bytes, which is quite small! It is smaller than the hicore that uses the core library core, but the difference is not big.

This is an executable file generated by static compilation without additional Runtime Library dependencies. Strictly speaking, it only depends on libc. so in the target system. Of course, if you do not need to output text to the console, you do not need libc. so.


Comparison of executable file sizes

Histd compiled using the Rust standard library std, hicore compiled using the Rust core library core, and hicore generated without the standard library or core library compilation, the size of executable files of the three is compared as follows:

-Rwxr-xr-x 1 liigo 1389884 September 17 20:20 histd-rwxr-xr-x 1 liigo 6556 September 17 20:19 hicore-rwxr-xr-x 1 liigo 6504 September 17 20:19 hi
The histd program compiled using the standard library std is too large and is not suitable for embedded devices. It is eliminated first. The hicore program compiled using the core library core is very small and comparable to the hicore program that does not use any library. Since core of the core library is not used, the file size remains unchanged, and core of the core library can bring a lot of coding convenience, it is recommended to use core of the core library in the embedded platform.

The above is the situation of the mipsel-linux platform. Below as a comparison, let's look at the x86_64-unknown-linux-gnu platform:

-Rwxr-xr-x 1 liigo 1004136 September 17 20:16 histd-rwxr-xr-x 1 liigo 8203 September 17 20:16 hicore-rwxr-xr-x 1 liigo 8159 September 17 20:16 hi

After comparison, it is found that the executable files on the x86_64 platform are slightly larger than those on the mipsel platform, but the gap is not big, and the situation is similar.


Target triples)

Information Source: https://github.com/rust-lang/rust/blob/master/mk/platform.mk

x86_64-unknown-linux-gnui686-unknown-linux-gnuarm-apple-iosi386-apple-iosx86_64-apple-darwini686-apple-darwinarm-linux-androideabiarm-unknown-linux-gnueabihfarm-unknown-linux-gnueabimipsel-linuxmips-unknown-linux-gnui586-mingw32msvci686-w64-mingw32x86_64-w64-mingw32x86_64-unknown-freebsdx86_64-pc-dragonfly-elf
Usage:

Compile Rust itself:./configure -- target = triple1, triple2 & make install

Compile the Rust program: rustc -- target = triple-C linker = triple-gcc

If necessary, create a symbolic link file, such as triple-gcc/triple-ar, for the SDK compilation tool of the Cross-compilation platform.


This article involves multiple source code files that have been uploaded to Github: https://github.com/liigo/hirust, author Liigo.


How can I change the system file in the openwrt route?

Linux: ssh root@192.168.x.x

In windows: Install putty, SSH secure shell client, etc.

How to read openwrt

Open w r t and WRT are three letters, which can be read separately. The following is an introduction to the encyclopedia. A good embedded operating system is applied to a wireless router, mainly on the MIPS platform.
OpenWrt is described as a Linux release of an embedded device, instead of trying to build a single, static firmware. OpenWrt package management provides a completely writable file system, select and configure from the application vendor and allow you to customize the device to adapt to any application by using the package. For developers, the OpenWrt framework is used to build applications without creating a complete firmware. for users, this means full customization, this device has never been used as expected.
After Linksys releases the source code of WRT54G/GS, many Firmware versions are available on the Internet to enhance the original functions. Most Firmware uses the Linksys source code 99%, and only 1% is added. Each Firmware is designed for a specific market, which has two disadvantages, the first is that it is difficult to combine the strengths of Firmware versions, and the second is that the version is getting farther and farther away from the official release of Linux.
OpenWrt selects another path. It starts from scratch and adds the software at to bring it closer to the Firmware function of Linksys, the success of OpenWrt is that its file system is writable. Developers do not need to re-compile it after every modification, making it more like a small Linux computer system, it also accelerates development.

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.