ARM 之七 主流编译器(armcc、iar、gcc for arm、LLVM(clang))详细介绍
必備
??在講解各編譯器之前,必須先了解一下以下這些文件。這些文件在編譯器目錄下或者編譯生成目標平臺的可執行程序時經常見到。此外,還需要注意區分 Windows 平臺 和 Linux 平臺的文件。
- .o 文件: 指的是 object 文件,俗稱目標文件。在 Linux 下擴展名縮寫為 .o,在 windows 下通常為 .obj 文件。
- .a 文件: 指的是 archive 文件,俗稱靜態庫文件。在 Linux 下擴展名縮寫為 .a,在 windows 下通常為 .lib 文件。
- .so 文件: 指的是 shared object 文件,用于動態連接的。在 Linux 下擴展名縮寫為 .so,在 windows 下通常為 .dll 文件。
??.o 文件是鏈接文件,.a 是靜態庫文件,需要 .o 文件生成,作為一個庫為外部程序提供函數接口。詳細的可以看一下博文 ARM 之一 鏡像文件(Image)/可執行文件/ELF文件/對象文件 詳解。
??在交叉編譯工具鏈目錄中,有大量的 .o 和 .a 文件。這些文件在我們編譯目標平臺時會被用到!為什么在 Windows 下面的編譯工具鏈中會有這么多的 .o 和 .a 文件呢?這是因為我們使用的這個編譯工具鏈是在 Linux 系統中編譯生成的! 。看下圖:
 
 如果有親自編譯過交叉編譯工具鏈,那么就一定會對 build、host 和 target 這幾個參數非常熟悉:
- –build=編譯該軟件(就是指的交叉編譯工具鏈本身)所使用的平臺
- –host=該軟件(就是指的交叉編譯工具鏈本身)將運行的平臺
- –target=該軟件(就是指的交叉編譯工具鏈本身)所要處理的目標平臺。即交叉編譯工具鏈編譯出來的程序運行的平臺。
比較
目前,針對于 ARM 平臺的主流編譯器主要有以下三者:
| 命令行工具 | 隨IDE發布,也獨立提供 | 僅隨其IDE發布,不獨立提供 | 獨立提供 | 只有命令行工具 | 
| 開發商 | ARM | IAR | ARM、Linaro、Mentor | LLVM | 
| 支持的平臺 | Windows、Linux | Windows | Windows、Linux、Mac(部分) | Windows、Linux、Mac | 
| 配套 IDE | Keil MDK、ARM Development Studio 5、ADS | IAR EMBEDDED WORKBENCH FOR ARM | 除以上兩者外的其他支持ARM的IDE,例如:eclipse、Visual Studio | 除以上兩者外的其他支持ARM的IDE,例如:eclipse、Visual Studio | 
| 官網 | https://developer.arm.com/tools-and-software/embedded/arm-compiler/downloads | https://www.iar.com/iar-embedded-workbench/ | 1. https://launchpad.net/gcc-arm-embedded 2. https://developer.arm.com/tools-and-software/open-source-software/developer-tools/gnu-toolchain/gnu-rm/downloads | https://llvm.org/ | 
ARM
??ARM 官網上除了提供了配套 IDE(參考博文《ARM 之 各集成開發環境(IDE)說明(Keil、RVDS、ADS、DS-5、MDK)》) 以外,每次都會提供編譯器的獨立下載。用戶可以單獨安裝編譯器,編譯器采用命令行形式使用。
 ??也就是說,ARM 的 IDE 和編譯器是相互獨立的,使用者可以為同一個 IDE 配置不同版本的編譯器!目前,ARM 官方提供兩個版本的編譯器,如下圖所示:
 
 ??其中,Arm Compiler 6 被 ARM 稱為是 Arm Compiler 5 的替代者!相比于 Arm Compiler 5 的使用的 Edison Design Group 編譯器前端,Arm Compiler 6 將編譯器前端換為了基于 LLVM 的 Clang!
 
 在 ARM 官網的介紹中,有如下一段話
 General update releases on the last branch, version 5.06, ended in H2 2017 with 5.06u6. After this, further support and maintenance will be available through Arm Compiler Long Term Maintenance releases with maintenance continuing until at least summer 2020. Arm Compiler 5.06 for Certification and Arm Compiler 5.06 Long Term Maintenance releases will each be supported by an Arm Compiler Qualification Kit.
 大意就是,Arm Compiler 5 以后就要退出舞臺了!在 ARM 內核的支持上,Arm Compiler 6 也要比 Arm Compiler 5 完善的多!
 目前,ARM 官方的 IDE 都會包含這兩個版本的編譯器。例如,在 Keil MDK 的安裝目錄下面會有如下兩個目錄,分別對應了 Arm Compiler 5 和 Arm Compiler 6
 
 上面說了,以上編譯器 ARM 也提供獨立下載安裝,具體見上文的官網地址即可!我本身安裝了多個版本的 ARM編譯器,如下:
 
這五個人依次是:Mike Miller, Daveed Vandevoorde, Steve Adamczyk, John Spicer, Mike Herricl。Daveed Vandevoorde 寫過兩本書《C++ Templates》和《C++ Solutions》,Mike Miller是C++專家,實現部分C++的功能。Mike Herrick在Bell實驗室呆了19年。Steve Adamczyk 和 John Spicer 是 EDG 的創建者。
armcc.exe
??ARM 指令和 Thumb? 指令編譯器。 用來編譯 C 和 C++代碼。它支持內聯和嵌入式匯編程序,還包括高級 SIMD 矢量化編譯器。
 ??編譯器支持將符合以下標準的 C 和 C ++ 源代碼編譯為 ARM 和 Thumb? 代碼:
- ISO Standard C:1990 source.
- ISO Standard C:1999 source.
- ISO Standard C++:2003 source.
- ISO Standard C++:2011 source.
armcc 符合 Base Standard Application Binary Interface for the ARM Architectur(ARM體系結構的基本標準應用程序二進制接口,BSABI):
- 生成 ELF 格式的對象文件。 參考博文《ARM 之 鏡像文件(Image)/可執行文件/ELF文件/對象文件 詳解》。
- 生成基于 Debug With Arbitrary Record Format Debugging Standard Version 3 (DWARF 3) 的調試信息 并且包含對于 DWARF 2 debug tables 的支持。
- 使用 Edison Design Group (EDG) 編譯器前端!
其默認的頭文件搜索路徑如下圖所示:
 
armasm.exe
?? ARM 和 Thumb 匯編器。用來匯編 ARM 和 Thumb 匯編語言源文件。
D:\ARM\ARM_Compiler_5.06u4>armasm Product: ARM Compiler 5.06 Component: ARM Compiler 5.06 update 4 (build 422) Tool: armasm [4d35cf] For Educational purposes only Software supplied by: ARM LimitedUsage: armasm [options] sourcefileOptions: --list listingfile Write a listing file (see manual for options) 生成列表文件-o outputfile Name the final output file 命名最終輸出文件名 --depend dependfile Save 'make' source file dependencies 保留 'make' 源文件依賴 --errors errorsfile Put stderr diagnostics to errorsfile 把標準錯誤判斷放入errorsfile-I dir[,dir] Add dirs to source file search path 添加源文件的搜索目錄 --pd --predefine directive Pre-execute a SET{L,A,S} directive 預執行 SET{L,A,S} 指令 --maxcache <n> Maximum cache size (default 8MB) 最大閃存空間 (default 8MB) --no_esc Ignore C-style (\c) escape sequences 忽略C風格(\ c)轉義序列 --no_warn Turn off Warning messages 關閉警告信息-g Output debugging tables 輸出調試表 --apcs /<quals> Make pre-definitions to match thechosen procedure-call standard 進行預定義以匹配選擇的程序調用標準 --checkreglist Warn about out of order LDM/STM register lists 警告LDM/STM寄存器列表出現故障 --help Print this information 打印幫助信息 --li Little-endian ARM 小端模式的 ARM --bi Big-endian ARM 大端模式的 ARM-M Write source file dependency lists to stdout 將源文件依賴關系列表寫入stdout --MD Write source file dependency lists to inputfile.d 將源文件依賴關系列表寫入inputfile.d --keep Keep local labels in symbol table of object file 將本地標簽保存在目標文件的符號表中 --regnames none Do not predefine register names 不預定義寄存器名稱 --split_ldm Fault long LDM/STM --unsafe Downgrade certain errors to warnings 將某些錯誤降級為警告 --via <file> Read further arguments from <file> 從<file>中讀取更多參數 --cpu <target-cpu> Set the target ARM core type 設置目標ARM核心類型 --cpu list Output a list of all the selectable CPUs 輸出所有可選CPU的列表 --fpu <target-arch> Set target FP architecture version 設置目標FP架構版本 --fpu list Output a list of all selectable FP architectures 輸出所有可選FP架構的列表 --thumb Assemble Thumb instructions 匯編 Thumb 指令 --arm Assemble ARM instructions 匯編 ARM 指令armlink.exe
??The linker. This combines the contents of one or more object files with selected parts of one ormore object libraries to produce an executable program.A 64-bit version of armlink is also provided that can access the greater amount of memoryavailable on 64-bit machines. It supports all the features that are supported by the 32-bit versionof armlink in this release. 連接器。用于將一個或多個目標文件的內容與一個或多個對象庫的選定部分組合在一起,以生成可執行程序。還提供了 64 位版本的 armlink,可以訪問 64 位計算機上可用的更大內存量。它支持此版本中 32 位版本的 armlink 支持的所有功能。
 ??If you are using ARM Compiler as a standalone product, then the 32-bit version is used bydefault. 如果您使用 ARM 編譯器作為獨立產品,則默認使用 32 位版本。
 ??For ARM Compiler in DS-5, the linker version depends on the host platform. 32-bit tools havethe 32-bit linker and 64-bit tools have the 64-bit linker. You do not get both versions.For the Microcontroller Developer Kit (MDK), only the 32-bit linker is provided. 對于 DS-5 中的 ARM 編譯器,鏈接器版本取決于主機平臺。 32 位工具具有32位鏈接器,64 位工具具有 64 位鏈接器。 您沒有獲得這兩個版本。對于微控制器開發工具包(MDK),僅提供 32 位鏈接器。
armar.exe
The librarian. This enables sets of ELF object files to be collected together and maintained inarchives or libraries. You can pass such a library or archive to the linker in place of several ELFfiles. You can also use the archive for distribution to a third party for further applicationdevelopment. 庫文件管理工具。 這使得 ELF 對象文件集可以一起收集并維護在原始文件或庫中。 您可以將此類庫或存檔傳遞給鏈接器以代替多個ELF文件。 您還可以使用存檔分發給第三方以進行進一步的應用程序開發。
D:\ARM\ARM_Compiler_5.06u4>armar Product: ARM Compiler 5.06 Component: ARM Compiler 5.06 update 4 (build 422) Tool: armar [4d35c8]Archive creation and maintenance toolCommand format:armar options archive [ file_list ]Wildcards '?' and '*' may be used in file_listOptions:--r Insert files in <file_list>, replace existing members of the same name. 在 <file_list> 中插入文件, 替換掉已經存在的同名成員。-d Delete the members in <file_list>. 從 <file_list> 中刪除成員。-x Extract members in <file_list> placing in files of the same name. 從 <file_list> 中提取同名的成員。-m Move files in <file_list>. 在 <file_list> 中移動文件。-p Print files to stdout. 打印文件到標準輸出設備。-a pos Insert/move files after file named <pos>. 插入/刪除 <pos> 后面的文件。-b pos Insert/move files before file named <pos>. 插入/刪除 <pos> 前面的文件。-u Update older files only, used with -r. 只更新舊的文件, 與 -r 一起使用。-n Do not add a symbol table to an object archive. 不要向 object 文件中添加符號表。-s Force regeneration of archive symbol table. 強制重新生成文檔符號表。-t Print table of contents of archive. 打印文檔的內容表。 --zs Show the symbol table. 顯示符號表。 --zt Summarize the archive contents (sizes + entries). 匯總文檔內容 (大小和輸入)。-c Suppress warning when a new archive is created. 當一個新文檔被創建的時候不顯示警告。-C Do not overwrite existing files when extracting. 提取的時候不要覆蓋一個已經存在的文件。-T Truncate file names to system maximum length. 截取系統最大長度文件名。-v Give verbose output. 提供詳細輸出。 --create Force creation of a new archive. 強制創建一個新文檔。 --via file Take additional arguments from via file. 從 via 文件中獲取額外參數。 --sizes List the size of each member and the library total. 列出所有成員大小與庫的總大小。 --entries List sections containing ENTRY points. 列出包括入口點的部分。 --vsn Print the current Armar Version. 打印最新的armar版本。 --help Print this message. 打印幫助信息。Examples:-armar -r mylib.a obj1 obj2 obj3...armar -x mylib.a ?sort*armar -d mylib.a hash.oarmar -tv ansilib.afromelf.exe
The image conversion utility. This can also generate textual information about the input image,such as its disassembly and its code and data size.鏡像轉換實用程序。 這還可以生成有關輸入圖像的文本信息,例如其反匯編及其代碼和數據大小。
D:\ARM\ARM_Compiler_5.06u4>fromelf Product: ARM Compiler 5.06 Component: ARM Compiler 5.06 update 4 (build 422) Tool: fromelf [4d35cb]ARM image conversion utility. ARM 鏡像轉換工具 fromelf [options] input_fileOptions:--help display this help screen 顯示幫助信息--vsn display version information 顯示版本信息--output file the output file. (defaults to stdout for -text format) 輸出文件名. (默認輸出 -text 格式)--nodebug do not put debug areas in the output image 不要輸出調試信息到映像文件中--nolinkview do not put sections in the output image 不要輸出段信息到映像文件中Binary Output Formats:--bin Plain Binary 普通二進制--m32 Motorola 32 bit Hex 摩托羅拉32位Hex碼--i32 Intel 32 bit Hex 英特爾32位Hex碼--vhx Byte Oriented Hex format 定向字節的 Hex 格式--base addr Optionally set base address for m32,i32 為 m32,i32設置基地址(可選的)Output Formats Requiring Debug Information 輸出格式要求的調試信息--fieldoffsets Assembly Language Description of Structures/Classes. Structures/Classes的匯編描述--expandarrays Arrays inside and outside structures are expanded. 展開內部和外部結構的數組Other Output Formats:--elf ELF ELF格式--text Text Information 文本信息Flags for Text Information 文本信息的標志-v verbose 詳細信息-a print data addresses (For images built with debug) 打印數據的地址信息 (得到的.axf映像文件)-c disassemble code 匯編碼-d print contents of data section 打印數據的段內容-e print exception tables 打印異常表-g print debug tables 打印調試表-r print relocation information 打印重定位信息-s print symbol table 打印符號表-t print string table 打印字符表-y print dynamic segment contents 打印動態段內容-z print code and data size information 打印代碼與數據的大小信息IAR
??相比于 ARM 對于編譯器的靈活安裝,IAR 的編譯器則只跟隨其 IDE 發布,編譯器不獨立提供。IAR 安裝后,目錄就是下面這個樣子了:
 
 這里我們重點關注一下arm目錄下的相關內容。其中內容有很多,這里主要介紹一下編譯器相關的那些,首先看下圖
 
 ??我們可以手動提取其編譯器,來作為命令行工具使用。但是對于 IAR 還是不建議這么用,個人感覺 IAR 在設計時,估計沒考慮過讓用戶使用命令行模式來獨立使用編譯套件,因為其編譯套件和其他好多東西都放在了一個目錄中,不像ARM 將編譯套件獨立存放。但是,IAR 的編譯套件是支持命令行使用的 ,在其介紹文檔中有這么一句話:The compiler, assembler, and linker can also be run from a command line environment,if you want to use them as external tools in an already established project environment. 官方的介紹文檔主要就是《 IAR C/C++ Development Guide Compiling and Linking》。
iasmarm.exe
??這個是 IAR 的匯編語言的編譯器。官方文檔是這么介紹的:The IAR Assembler for Arm is a powerful relocating macro assembler with a versatile set of directives and expression operators. The assembler features a built-in C language preprocessor and supports conditional assembly. 大意就是: IAR Assembler for Arm 是一個功能強大的重定位宏匯編程序,具有多種指令和表達式運算符。 匯編程序具有內置的 C 語言預處理程序,并支持條件匯編。該工具官方有個獨立的說明文檔《IAR Assembler Reference Guide》,里面有該工具的詳細使用說明。
E:\Program Files (x86)\IAR Systems\Embedded Workbench 8.2\arm\bin>iasmarmIAR Assembler V8.32.3.193/W32 for ARMCopyright 1999-2019 IAR Systems AB.Usage: iasmarm {<options>} <sourcefile> {<options>} Sourcefile: source file with default extension: .msa, , .asm, or .sEnvironment: IASMARM Options (specified order is of no importance): -B Print debug info for assembler macros -c{DEAOM} Listing optionsD: Disable listing, E: Disable macro expansionA: List only assembled part O: List several lines of codeM: List Macro definition -DSYMB Equivalent to: #define SYMB 1 -DSYMB=xx Equivalent to: #define SYMB xx -e Use big-endian byte order -Enumber Allow <number> errors -f file Extend command line with <file> <.xcl> -g No system include -G Open standard input as source -i List #included files -Ipath Add #include search path -j Enable alternative register names, operators and mnemonics -l file Generate a list on: <file> <.lst> -Lpath Generate a list on: <path> \ <source> <.lst> -Mab Change asm.macro argument quote chars,where a is start-of-quote and b is end-of-quote char.default is a == < and b == >. -N No header in listing -o file Put object on: <file> <.o> -Opath Put object on: <path> \ <source> <.o> -pnn Page listing with 'nn' lines/page (10-150) -r Enable debugger output in object -S Silent operation of assembler -s{+|-} Set case sensitivity for user symbols-s and -s+ enables sensitivity, -s- disables it. -tn Set tab spacing between 2 and 9 (default 8) -USYMB Equivalent to: #undef SYMB -ws To make warnings generate exit code 1 -wstring Disable warningsstring: <+|-,><+|-range><,+|-range>...range: low_warning_nr<-high_warning_nr>example: -w turns all warnings off-w-,+10-12,+20 turns all but 10,11,12 and 20 off-x{DI2} Generate cross-reference listD: Show all #defines, I: Show Internal table2: Dual line space listing --aarch64 Generate code for AArch64, same as --cpu_mode A64--abi {lp64|ilp32}Specify ABI for AArch64: ilp32 or lp64. --arm Generate code in arm mode, same as --cpu_mode arm--cmseTarget secure mode in CMSE (ARMv8-M security extensions) --cpu core Specify target coreValid options are core names such as Cortex-M3and architecture names such as 7MDefault is Cortex-M3 --cpu_mode {arm|a|thumb|t}Select default mode for CODE directive, ARM is default--endian {little|l|big|b}Specify target byte order --fpu {VFPv1|VFPv2|VFPv3{_D16}{_FP16}|VFP9-S|none}Specify target FPU coprocessor supportDefault is none, which selects the softwarefloating-point library. --legacy {legacyOption}Generate object files compatible witholder toolchains. Valid options are:RVCT3.0 --no_dwarf3_cfiSuppress Dwarf 3 Call Frame Information instructions --no_it_verificationDo not verify that the instructions followingan IT instruction has the correct condition set--no_literal_poolUse MOV32 for LDR Rd,=expr (requires ARMv7-M) --no_path_in_file_macrosStrip path from __FILE__ macros --source_encoding {locale|utf8} Encoding to use for source files with no BOM --suppress_vfe_headerDo not generate VFE header info--system_include_directory <path>Set system header directory --thumb Generate code in thumb mode, same as --cpu_mode thumb--version Output version info and exiticcarm.exe
??這個是 IAR 的 C/C++ 編譯器。官方文檔是這么介紹的:The IAR C/C++ Compiler for Arm is a state-of-the-art compiler that offers the standard features of the C and C++ languages, plus extensions designed to take advantage of the Arm-specific facilities.大意就是: IAR C / C ++ Compiler for Arm是一個最先進的編譯器,提供 C 和 C++ 語言的標準功能,以及旨在利用 Arm 特定功能的擴展。
 ??默認使用的頭文件目錄.\arm\inc\<vendor>
IDE中有兩個工具可將應用程序源文件轉換為中間文件目標文件。 IAR C / C ++編譯器和IAR匯編器。 兩者都產生行業標準格式ELF中的可重定位目標文件,包括DWARF調試信息的格式。下圖顯示了編譯過程:
 
ilinkarm.exe
??這個是 IAR 的連接器。官方文檔是這么介紹的:The IAR ILINK Linker for Arm is a powerful, flexible software tool for use in the development of embedded controller applications. It is equally well suited for linking small, single-file, absolute assembler programs as it is for linking large, relocatable input, multi-module, C/C++, or mixed C/C++ and assembler programs. 大意就是:IAR ILINK Linker for Arm 是一款功能強大,靈活的軟件工具,可用于嵌入式控制器應用程序的開發。 它同樣適用于鏈接小型,單文件,絕對匯編程序,因為它用于鏈接大型可重定位輸入,多模塊,C/C++ 或混合 C/ C++ 和匯編程序。
 ??ilinkarm 使用并生成行業標準的 ELF 和 DWARF 作為對象格式文件。在.\arm\config目錄下,包含了針對各平臺的連接器使用的配置文件。
IAR ILINK鏈接器(ilinkarm.exe)用于構建最終應用程序。通常,鏈接器需要以下信息作為輸入:
- 幾個目標文件,可能還有某些庫
- 程序開始標簽(默認設置)
- 鏈接器配置文件,用于描述目標系統內存中代碼和數據的放置
下圖顯示了鏈接過程:
 
 IAR ILINK鏈接器生成ELF格式的絕對目標文件,其中包含可執行鏡像。 鏈接后,可以使用生成的絕對可執行映像
- 加載到IAR C-SPY調試器或任何其他兼容的外部調試器讀取ELF和DWARF。
- 使用flash / PROM編程器對flash / PROM進行編程。 在此之前可能必須使用ielftool將鏡像中的實際字節轉換為標準的Motorola 32-bit S-record 格式或Intel Hex-32格式。
iarchive.exe
??檔案管理工具,類似于 ARM 的 armar 和 GCC 的 ar。用于創建和操作幾個ELF目標文件的庫(存檔)。庫文件包含多個可重定位的 ELF 對象模塊,每個模塊都可以由鏈接器獨立使用。 與直接指定給鏈接器的對象模塊相比,只有在需要時才包含庫中的每個模塊。
E:\Program Files (x86)\IAR Systems\Embedded Workbench 8.2\arm\bin>iarchiveIAR Archive Tool V10.4.14.1149Copyright 2008-2019 IAR Systems AB.Usage: iarchive [command] archive obj1 ... objNiarchive [command] obj1 ... objN -o archiveiarchive [command] archiveAvailable command line options: --create Create new archive --delete -d Delete module(s) from archive --extract -x Extract module(s) from archive -f file Read command line options from file --f file Read command line options from file and report dependency --no_bom Don't use a Byte Order Mark in Unicode output --output archive -o archive Name of archive file --replace -r Replace or add module(s) to archive --symbols List symbol table of archive --text_out encodingEncoding to use for text output filesutf8 UTF-8utf16le Little-endian UTF-16utf16be Big-endian UTF-16locale Locale specific encoding --toc -t List archive table of content --utf8_text_in Non-source text input files with no BOM use UTF-8 encoding --verbose -V verbose operation --version Output version information and exit --vtoc List archive table of content (verbose)使用示例:
ielftool.exe
??ARM ELF 文件工具,類似于 ARM 的 fromelf 和 GCC 的 elfedit。對 ELF 可執行映像執行各種轉換(例如,填充,校驗和,格式轉換等)。安裝目錄.\arm \ src\elfutils下提供了 ielftool 源代碼(Microsoft VisualStudio項目)。 如果對如何生成校驗和或格式轉換要求有特定要求,則可以相應地修改源代碼。
E:\Program Files (x86)\IAR Systems\Embedded Workbench 8.2\arm\bin>iarchiveIAR Archive Tool V10.4.14.1149Copyright 2008-2019 IAR Systems AB.Usage: iarchive [command] archive obj1 ... objNiarchive [command] obj1 ... objN -o archiveiarchive [command] archiveAvailable command line options: --create Create new archive --delete -d Delete module(s) from archive --extract -x Extract module(s) from archive -f file Read command line options from file --f file Read command line options from file and report dependency --no_bom Don't use a Byte Order Mark in Unicode output --output archive -o archive Name of archive file --replace -r Replace or add module(s) to archive --symbols List symbol table of archive --text_out encodingEncoding to use for text output filesutf8 UTF-8utf16le Little-endian UTF-16utf16be Big-endian UTF-16locale Locale specific encoding --toc -t List archive table of content --utf8_text_in Non-source text input files with no BOM use UTF-8 encoding --verbose -V verbose operation --version Output version information and exit --vtoc List archive table of content (verbose)E:\Program Files (x86)\IAR Systems\Embedded Workbench 8.2\arm\bin>ielftoolIAR ELF Tool V10.4.14.1149 [BUILT at IAR]Copyright 2007-2019 IAR Systems AB.Usage: ielftool input_file output_fileAvailable command line options: --bin Save as raw binary --checksum sym:size,algo[:[1|2][a|m|z][r][R][o][x][i|p][W|L]][,start];range[;range...]Generate checksumsym Checksum symbolsize Length of the symbol in bytesalgo Algorithm: sum, sum8wide, sum32, crc16, crc32crc64iso, crc64ecma or crc=poly1|2 Complement: 1 or 2a|m|z Reverse the bit order for:input bytes only: ainput bytes and final checksum: mfinal checksum only: zo Output the Rocksoft model specificationr Reverse the byte order within each wordR Traverse the range(s) in reverse orderx Toggle the endianess of the checksumi|p Use initial value normally: iPrefix input data with the start value: pW|L Use a checksum unit length of 2 bytes: WUse a checksum unit length of 4 bytes: Lstart Initial checksum value (defaults to 0)range Do checksum of bytes in range --fill [v;]pattern;range[;range...]Specify fillv Virtual fill, do not generate actual filler bytes.This can be used for checksums and parities.pattern Sequence of filler bytesrange Fill range --front_headers Move program and section headers to the front of the ELF file. --ihex Save as 32-bit linear Intel Extended hex --offset [-]offsetAdd (subtract if - is used) offset to all address records.This only works for the output formats: Motorola S-records,Intel Hex, Simple-Code and TI-TXT --parity sym:size,algo:flashbase[:[r][[B|W|L]];range[;range...]Generate parity bitssym Parity symbolsize Length of the symbol in bytesalgo Parity algorithm: odd, evenflashbase Ignore bytes before this addressr Traverse the range(s) in reverse orderB Use a parity unit length of 1 byteW Use a parity unit length of 2 bytesL Use a parity unit length of 4 bytesrange Perform parity on bytes in this range --self_reloc relocator[,jtc]Create self-relocating image with relocatorjtc Number of jump table entries --silent Silent operation --simple Save as Simple-code --simple-ne Save as Simple-code without entry record --srec Save as Motorola S-records --srec-len lengthRestrict the length of S-records --srec-s3only Restrict the type of S-records to S3 (and S7) --strip Remove all section headers and non-program sections --titxt Save as Texas Instruments TI-TXT --verbose Print all performed operations --version Output tool version使用示例:
ielfdumparm.exe
??針對 ARM ELF 格式的文件的 Dumper工具。類似于 GCC 的 objdump,用于創建ELF可重定位或可執行映像內容的文本表示。主要用于以下三個方面:
- To produce a listing of the general properties of the input file and the ELF segments and ELF sections it contains. This is the default behavior when no command line options are used.生成輸入文件的常規屬性列表以及它包含的ELF段和ELF節。 當沒有使用命令行選項時,這是默認行為。
- To also include a textual representation of the contents of each ELF section in the input file. To specify this behavior, use the command line option --all .還包括輸入文件中每個ELF部分內容的文本表示。 要指定此行為,請使用命令行選項–all。
- To produce a textual representation of selected ELF sections from the input file. To specify this behavior, use the command line option --section 從輸入文件生成所選ELF節的文本表示。 要指定此行為,請使用命令行選項–section
iobjmanip.exe
針對 ARM ELF 格式的 Object 文件的操作工具。用于執行ELF目標文件的低級操作。
E:\Program Files (x86)\IAR Systems\Embedded Workbench 8.2\arm\bin>iobjmanipIAR Object File Manipulator V10.4.14.1149Copyright 2009-2019 IAR Systems AB.Usage: iobjmanip <op1>[,...<opN>] <src> <dest>Available command line options: -f file Read command line options from file --f file Read command line options from file and report dependency --no_bom Don't use a Byte Order Mark in Unicode output --remove_file_pathremove path information from file symbol --remove_section #|nameremove matching section(s) --rename_section (#|name)=namerename matching section(s) --rename_symbol name=namerename matching symbol --strip strip debug information --text_out encodingEncoding to use for text output filesutf8 UTF-8utf16le Little-endian UTF-16utf16be Big-endian UTF-16locale Locale specific encoding --utf8_text_in Non-source text input files with no BOM use UTF-8 encoding --version Output version information and exit使用示例:
isymexport.exe
絕對符號導出器。 從ROM映像文件中導出絕對符號,以便在鏈接附加應用程序時使用它們。
E:\Program Files (x86)\IAR Systems\Embedded Workbench 8.2\arm\bin>isymexportIAR Absolute Symbol Exporter V10.4.14.1149Copyright 2008-2019 IAR Systems AB.Usage: ISymExport input_file output_fileAvailable command line options: --edit steering_fileShow/hide/rename symbols --export_locals[=symbol_prefix]Export local variable and function symbols -f file Read command line options from file --f file Read command line options from file and report dependency --generate_vfe_headerGenerate vfe header section --no_bom Don't use a Byte Order Mark in Unicode output --ram_reserve_ranges[=symbol_prefix]Generate symbols to reserve all occupied RAM ranges --reserve_ranges[=symbol_prefix]Generate symbols to reserve all occupied ranges --show_entry_as[=name]Export the entry point of the program as name --text_out encodingEncoding to use for text output filesutf8 UTF-8utf16le Little-endian UTF-16utf16be Big-endian UTF-16locale Locale specific encoding --utf8_text_in Non-source text input files with no BOM use UTF-8 encoding --version Output version information and exitiexe2obj.exe
IAR ELF可重定位對象創建器。從可執行的ELF目標文件創建可重定位的ELF目標文件。
E:\Program Files (x86)\IAR Systems\Embedded Workbench 8.2\arm\bin>iexe2objIAR ELF Exe to Object Tool V10.4.14.1149Copyright 2008-2019 IAR Systems AB.Usage: IExe2Obj input_file output_fileAvailable command line options: -f file Read command line options from file --f file Read command line options from file and report dependency --hide_symbols Hide all symbols in the image --keep_mode_symbolsKeep mode symbols in the image --no_bom Don't use a Byte Order Mark in Unicode output --prefix prefix Set section/symbol name prefix --text_out encodingEncoding to use for text output filesutf8 UTF-8utf16le Little-endian UTF-16utf16be Big-endian UTF-16locale Locale specific encoding --utf8_text_in Non-source text input files with no BOM use UTF-8 encoding --version Output version information and exit --wrap function Create wrapper for functionGCC for ARM
??GCC 原名為 GNU C 語言編譯器(GNU C Compiler),因為它原本只能處理 C 語言。不過,后來 GCC 被進行了擴展,變得可處理 C++。后來又擴展能夠支持更多編程語言,如 Fortran、Pascal、Objective-C、Java、Ada、Go 以及各類處理器架構上的匯編語言等,所以改名 GNU 編譯器套件(GNU Compiler Collection)。 更名之后,原來的針對于 C 語言的編譯器名字還叫 gcc,針對 C++ 的編譯器叫做 g++ 。
??GCC for ARM(這個名字是我自己起的,用來代指所有基于 GCC 的針對 ARM 平臺的編譯套件) 是基于 GCC 開發的,用來編譯生成 ARM 內核可執行文件的編譯套件,也叫 ARM 交叉編譯工具鏈。 相比于以上兩個巨貴的編譯器,GCC for ARM 因為是基于開源的 GCC 的,因此是免費的。目前主要由三大主流工具商提供,第一是 ARM,第二是 Codesourcery,第三是 Linora。目前我們用的針對 ARM 芯片的集成開發環境(IDE),除了 IAR 和 ARM 自己的 Keil、DS ,大多都是使用 GCC for ARM 的編譯器!
首先,看看 ARM 交叉編譯工具鏈的命名規則:arch [-vendor] [-os] [-(gnu)eabi] [-gcc]
- arch: 體系架構,如 ARM,MIPS
- vendor: 工具鏈提供商,沒有 vendor 時,用 none 代替;
- os: 目標操作系統,沒有 os 支持時,也用 none 代替
- eabi: 嵌入式應用二進制接口(Embedded Application Binary Interface)
如果同時沒有 vendor 和 os 支持,則只用一個 none 代替。例如 arm-none-eabi 中的 none 表示既沒有 vendor 也沒有 os 支持。 前面說過,GCC for ARM 是基于 GCC 開發的。因此,其和 GCC 一樣是一套命令行工具的集合,理論上可以將它集成到其他任何集成開發環境中,從而不直接使用命令行。GCC for ARM 中的各命令行工具與 GCC 中的各命令行工具都是對應的,功能基本一致,僅僅是名字有些改變!
??基于 GCC 的 ARM 編譯工具鏈提供商有 ARM、Codesourcery、Linaro 這三家,但其中使用最多還是 ARM 提供的 GCC 編譯器。下面我們分別簡單來介紹一下這三家的編譯工具鏈。
Arm GNU Toolchain
??ARM 除了有自己的專用編譯器之外,還維護了一套基于 GCC 的交叉編譯工具鏈,被稱為 Arm GNU Toolchain。估計是為了能更有效的占有市場吧!絕大多數第三方的 IDE 都是使用這一套交叉編譯工具鏈。
??注意,在 2022 年之前,Arm GNU Toolchain 被分為了 A-profile(GNU Toolchain for A-profile processors) 和 R & M profiles(GNU Arm Embedded Toolchain)兩大類,但是從 2022 年開始統一為了一個,之前的已經停止開發。2022 年第一版叫做 Arm GNU Toolchain Version 11.2-2022.02。
??在 2022 年以前 R & M profiles 編譯工具鏈的名字只有 arm-none-eabi,只能編譯裸機平臺,Cortex-A 則有裸機和 Linux 版之分的多種編譯工具鏈;在 2022 年開始,則根據架構及是否支持 Linux 系統進行了大一統,我們可以根據需要選擇合適的版本。
下面這兩個章節還是 2022 年以前的 Arm GNU Toolchain 的介紹。2022 年以前的 Arm GNU Toolchain 官網還提供下載,只是不再進行更新。
arm-none-eabi
??用于編譯 ARM 架構的裸機系統(包括 ARM Linux 的 boot、kernel,不適用編譯 Linux 應用 Application),所以不支持那些跟操作系統關系密切的函數,比如 fork,它使用的是 newlib 這個專用于嵌入式系統的 C 庫。這是目前我們編寫 ARM 裸機程序時,使用最多的交采編譯工具鏈! 安裝/解壓 之后,目錄如下圖所示:
 
 ??編譯器工具中的各工具,與標準的 GCC 沒有太多區別,主要就是針對的平臺變了。各工具的功能是一樣的!比如:arm-none-eabi-gcc.exe 是C 語言編譯器、arm-none-eabi-g++.exe 是 C++ 編譯器、arm-none-eabi-ld.exe 是連接器、arm-none-eabi-gdb.exe 是調試器等等。
?? 在很久以前,ARM 使用 launchpad 來維護該項目源碼。但是根據之前的公告,launchpad 上不發布編譯好的程序和源碼包(“As previously announced all new binary and source packages will not be released on Launchpad henceforth, they can be found on:
 https://developer.arm.com/open-source/gnu-toolchain/gnu-rm.”),只能從 ARM 官網:https://developer.arm.com/tools-and-software/open-source-software/developer-tools/gnu-toolchain/downloads 進行下載,launchpad 僅用于 BUG 提交等。
Cortex-A 專用
??以上交叉編譯工具鏈只支持 ARM Cortex-M/R 等系列的核,ARM 官網還提供了針對于 ARM Cortex-A 系列內核的交叉編譯工具鏈,可以從以下地址下載
 https://developer.arm.com/tools-and-software/open-source-software/developer-tools/gnu-toolchain/gnu-a/downloads。 具體如下所示:
 
 需要注意的是,上圖中紅框全稱的就是編譯工具鏈的名字。各命令行工具與標準 GCC 也沒啥區別!再一個需要注意的是,編譯器的目標平臺。
- AArch32 bare-metal target:32 位純裸機平臺
- AArch64 ELF bare-metal target:64 位純裸機平臺
- AArch64 ELF bare-metal, big-endian target:64 位純裸機平臺(大端模式)
- AArch64 GNU/Linux target:64 位 Linux 平臺
- AArch64 GNU/Linux big-endian target:64 位 Linux 平臺(大端模式)
- AArch32 target with soft float:32 位帶軟件模式浮點運算
- AArch32 target with hard float:32 位帶硬件模式浮點運算
Codesourcery Toolchain
??Codesourcery推出的產品叫 Sourcery G++ Lite Edition,其中基于 command-line 的編譯器是免費的,在官網上可以下載,而其中包含的 IDE 和 debug 工具是收費的。Codesourcery 公司(目前已經被 Mentor 收購)基于 GCC 推出的 ARM 交叉編譯工具。可用于交叉編譯 ARM MCU 芯片,如 ARM7、ARM9、Cortex-M/R 芯片程序。
 ??目前 CodeSourcery 已經由明導國際 (Mentor Graphics) 收購,所以原本的網站風格已經全部變為 Mentor 樣式,這貨被收之后,不知道怎么下載其編譯工具鏈。。。
- arm-none-linux-gnueabi-gcc: 用于交叉編譯 ARM(32位)系統中所有環節的代碼,包括裸機程序、u-boot、Linux kernel、filesystem和App應用程序。
- arm-none-elf-gcc: 用于交叉編譯 ARM MCU(32位)芯片,如 ARM7、ARM9、Cortex-M/R 芯片程序。
Linaro Toolchain
?? Linaro 是在 2010 年臺北國際計算機展 ( COMPUTEX ) 期間,ARM、Freescale、Samsung、ST-Ericsson、德州儀器(TI)與 IBM 等 6 家大廠,宣布合資成立的非贏利 Linux 基礎架構軟件研發商。其基于 GCC 推出的 ARM 交叉編譯工具如下圖所示:
 
 下載地址為:https://www.linaro.org/downloads/ 。從上圖不難看出,Linaro 提供的交叉編譯環境,僅針對于 Cortex-A 內核,其他 ARM 內核則需要去 ARM 官網下載!
- aarch64-linux-gnu: 針對于目標平臺是 Linux 系統,用于交叉編譯 ARMv8 64 位目標中的裸機程序、u-boot、Linux kernel、filesystem 和 App 應用程序。
- arm-linux-gnueabihf: 針對于目標平臺是 Linux 系統,用于交叉編譯ARM(32位)系統中所有環節的代碼,包括裸機程序、u-boot、Linux kernel、filesystem和 App 應用程序。
- arm-eabi-gcc: 用于編譯 ARM 架構的裸機系統,包括 ARM Linux 的 boot、kernel,不適用編譯 Linux 應用 Application
- aarch64-elf: 用于編譯 ARM v8 64位架構的裸機系統,包括 ARM Linux 的 boot、kernel,不適用編譯 Linux 應用 Application
??正如官網的說明,官方發布的編譯好二進制可執行編譯器文件,僅在 Linux 系統( Ubuntu LTS)進行了測試。 目前,官方沒有提供其他平臺的可執行程序!
參考
總結
以上是生活随笔為你收集整理的ARM 之七 主流编译器(armcc、iar、gcc for arm、LLVM(clang))详细介绍的全部內容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: USB 之四 USB 发展(更名)史 /
- 下一篇: ARM 之八 Cortex-M/R 内核
