前言
這個世界上,須要玩虛的;也須要玩實的;
活命須要玩實的。但不必定活的好;不必定活的開心;但能夠活下去。
最高境界是玩虛的;但前提是銀行賬號裏銀子要是實在的。。。
《系統軟件工程師實戰攻略(體系結構篇)》 的目的爲嵌入式系統工程師,網絡工程師,提供一些體系結構實戰案例。目的是活命。全部的代碼都是實踐調試過的,具備產品級別的質量。版權是BSD License。換言之,隨便重用。
這些東東對大遼,大宋的工程師們,菸酒生們都適用。
之後再整《系統軟件工程師實戰攻略(操做系統篇)》和《系統軟件工程師實戰攻略(網絡篇)》
而後就封筆了。
但願有所幫助,
陳懷臨,2011 12 8 ,加州
如何作一個交叉編譯器(cross compiler)和工具鏈
要玩嵌入式,玩CPU和板子,首先要學會定製一個交叉編譯器。交叉編譯器是啥意思呢? 其實就是在你的研發的機器上(例如一個x86機器)
,作研發,寫代碼。而後用這個編譯器去產生相應的代碼(例如,MIPS,Arm,或者PowerPC,或者龍芯。。。。。。)
GCC和ToolChain包括gcc,gdb,tools三大塊。源碼均可以從GNU去下載。而後本身修改和定作。
相關的下載地址爲:
GCC: http://gcc.gnu.org/releases.html
GDB: http://www.gnu.org/s/gdb/download/
binutil:http://www.gnu.org/software/binutils/
libc: http://sourceware.org/newlib/
下面是一個完整的script能夠用來作MIPS,PowerPC,ARM和任何其餘目標CPU的cross compiler。我作的這些東東是2004年作和整理的。能夠看見各類版本與目前最新的偏低了。
/*
* Copyright (c) 2011, Huailin Chen
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by
* the Huailin Chen and its contributors.
* 4. Neither the name of the Huailin Chen nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS 「AS IS」 AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* Creation Date: 1/14/2004
* Author: Huailin Chen huailin@gmail.com
*
* $Id: build_script,v 1.1 2004/05/07 17:41:48 huailin Exp $
*/
########################################
PWD =
CPU =
########################################
#把下載的各個package展開後放在一個gnu_download目錄裏
#4個子目錄,分別存放gcc,gdb,libc和binutil的原文件
BINUTILS_DIR =./gnu_download/binutils-2.9.1
GCC_DIR =./gnu_download/gcc-2.95.1
LIBC_DIR =./gnu_download/newlib-1.8.2
GDB_DIR =./gnu_download/gdb-5.0
###################################
TARGET_ARM =arm-elf
TARGET_MIPS_ELF=mips-elf
TARGET_PPC_EABI_ELF=powerpc-eabi
TARGET_SH_ELF =arm-elf
#TARGET_AMD
#TARGET_ALPHA
#TARGET_IA32
###################################
#PREFIX目錄是最後的結果存放目錄。要事先建好
#
PREFIX=$(PWD)/../arch/$(CPU)/tool-chain
TARGET = $(TARGET_ARM)
####################################
mkdir build-binutils build-gcc build-newlib build-gdb
cd build-binutils
$(BINUTILS_DIR)/configure –target=$(TARGET) –prefix=$(PREFIX) -v
make all install
pwd
# Configure, build and install gcc
cd build-gcc
$(GCC_DIR)/configure –target=$(TARGET) –prefix=$(PREFIX) –with-newlib –with-headers=$(PWD)/gnu_download/$(LIBC)/newlib/libc/include –with-gnu-as –with-gnu-ld –enable-languages=」c」
-v
make all install
#Configure, build and install libc
pwd
cd ../build-newlib
$(LIBC_DIR)/configure –target=$(TARGET) –prefix=$(PREFIX) -v
make all install
#Configure, build and install gdb
pwd
cd ../build-gdb
$(GDB_DIR)/configure –target=$(TARGET) –prefix=$(PREFIX) -v
make all install
#Clear history directory/files
pwd
cd ..
rm -rf build-*
如何掌握一個CPU的ABI(Application Binary Interface)html
在體系結構中,一般會說一個CPU的結構(Architecture),一個CPU的微結構(Micro-Architecture)。linux
是啥意思呢?程序員
結構的意思就是那些程序員能看見的東東;微結構就是程序員不(太)須要關心的東西。web
對能看得見的結構部分(例如,通用寄存器,控制寄存器,狀態寄存器,中斷或者例外寄存器)的使用方法,或者說編程模式,就是所謂的ABI。編程
掌握一個CPU的ABI,或者說編程界面,是一個基本功。是必須的。淺顯說,就是那些寄存器的用法,分佈和使用約定。後端
定義一個處理器的ABI,也是作編譯器設計的第一個環節。筆者曾經設計過一個網絡處理器的GCC的後端target。設計的第一個事情其實就是設計寄存器的約定。網絡
這個章節裏,主要具體談如何掌握MIPS,PowerPC,Arm和x86的ABI。ide
一些相關的中文參考資料以下:工具
《PowerPC and Linux Kernel Inside》, 陳懷臨,2002ui
《MIPS CPU 體系結構概述,Linux/MIPS內核》(上),陳懷臨,張富新,2002
《MIPS CPU 體系結構概述,Linux/MIPS內核》(下),陳懷臨,張富新,2002
《Linux PowerPC詳解:核心篇》, 王齊, 2008[有沒有網上電子版?]
《Arm 體系結構》,李曦