1. COFF format 1> Common Object File Format is a popular binary executable file format. Binary executable files include library files (lib), object files (obj) and final executable files (out). The binary file format (PE) of the operating system after Windows 95 and NT4.0 on today's PC is a further expansion of the COFF format. 2> COFF format: The detailed COFF file format includes segment headers, executable code and initialization data, relocatable information, line number entry, symbol table, string table, etc. These belong to the category of concern for those who write operating systems and compilers. For C, you only need to understand the definition of segments and the allocation of space to segments. 3> Using COFF is more conducive to modular programming. Programmers can freely decide which codes they want to belong to which segments, and then treat them differently. 2. Section The smallest unit in the target file is called a block. A block is a section of code or data that eventually occupies a continuous space in the memory image. 1> COFF object files contain three default blocks: .text executable code .data initialized data .bss space reserved for uninitialized data 2> Assembler processing of blocks Uninitialized block .bss variable storage space .usect User-defined uninitialized section Initialization block .text Assembly instruction code .data Constant data (such as initialization data for variables) .sect User-defined initialized segment .asect .sect has the absolute address positioning function, which is generally not used 3>C language segment Uninitialized block (data) .bss Stores global and static variables .ebss Long call .bss (exceeds the 64K address limit) .stack Stores the C language stack .sysmem Stores the C language heap .esysmem .sysmem for long calls (exceeds the 64K address limit) initialization block .text executable code and constants (program) .switch Constant table generated by switch statement (program/low 64K data space) .pinit Tables for global constructors (C++)(program) .cinit Used to store initialization constant values for global and static variables (program) .const Global and static const variable initialization values and string constants, (data) .econst long.const (can be located anywhere) (data) 3> Custom segment (C language) #pragma DATA_SECTION(function name or global variable name,"User-defined segment name in data space"); #pragma CODE_SECTION(function name or global variable name,"User-defined segment name in program space"); Cannot be declared in the function body. Must be declared before definition and use #pragma can prevent optimization of uncalled functions 3.Connection command file (CMD) 1> MEMORY specifies storage space MEMORY { PAGE 0: name 0 [attr] : origin = constant, length = constant PAGE n: name n [attr] : origin = constant, length = constant } PAGE n: indicates storage space, n<255; PAGE 0 is program storage space; PAGE 1 is program storage space name: storage space name attr: Storage space attributes: read-only R, write-only W, can contain executable code X, can be initialized I. orgin: used to define the starting address of the storage space Lenth: used to define the length of the storage space 2> SECTIONS allocation segment SECTIONS { name : [property,property,……] } name: the name of the output segment property: the properties of the output segment: load = allocation (forced address or storage space name) same as >allocation: defines where the output segment will be loaded. run= allocation (forced address or storage space name) same as >allocation: defines where the output segment will be run. Note: When only one keyword load or run appears in the CMD file, the addresses of the two are the same. PAGE = n, the segment is located in that storage page space. Example: ramfuncs: LOAD = FLASHD, RUN = RAML0, LOAD_START(_RamfuncsLoadStart), LOAD_END(_RamfuncsLoadEnd), RUN_START(_RamfuncsRunStart), PAGE = 0 3> Directly write the compilation command -l rts2800_ml.lib links the system file rts2800_ml.lib -o filename.out The final binary file is named filename.out -m filename.map Generates mapping file filename.map -stack 0x200 The stack is 512 words 4. .const section: The initialization value of global variables qualified by the keyword const (const-qualified local variables are not generated), and string constants appearing in expressions (used as pointers, but not used to initialize string array variables). In addition, when arrays and structures are local variables, their initial values will generate a .const section, but not when they are global. --------------------------------------------------------------------------------------------------------------------------- CMD is used to allocate ROM and RAM space, telling the linker how to calculate addresses and allocate space. So different chips have different sizes of ROM and RAM. The places where user programs are placed are also different. So you need to modify it according to your chip. It is divided into two parts: MEMORY and SECTIONS. MEMORY { PAGE 0 .......... PAGE 1......... } SECTIONS {SECTIONS { .vectors ............. .reset ................. ................ } MEMORY is used to specify the size of the chip's rom and ram and divide it into several intervals. PAGE 0 corresponds to rom;PAGE 1 corresponds to ram The interval name contained in PAGE and the parameters following it reflect the starting address and length of the interval. For example: PAGE 0: VECS(interval name): origin(starting address) = 0h, length (length) =040h SECTIONS: (Add the following segment name in the program, such as .vectors. Used to specify below this segment name and above another segment name The program (belonging to PAGE0) or data (belonging to PAGE1) is placed in the space name after the “>” symbol. For example, the program or data that references the field name “.vectors” will be placed in VECS. VECS is PAGE0, which is the ROM space from 00H to 40H SECTIONS { .vectors : { } > VECS PAGE 0 .reset : { } > VECS PAGE 0 ............ ............ .......... } Example: MEMORY { PAGE 0: VECS: origin = 00000h, length = 00040h LOW: origin = 00040h, length = 03FC0h SARAM: origin = 04000h, length = 00800h B0: origin = 0FF00h, length = 00100h PAGE 1: B0: origin = 00200h, length = 00100h B1: origin = 00300h, length = 00100h B2: origin = 00060h, length = 00020h SARAM: origin = 08000h, length = 00800h } SECTIONS { .text : { } > LOW PAGE 0 .cinit : { } > LOW PAGE 0 .switch : { } > LOW PAGE 0 .const : { } > SARAM PAGE 1 .data : { } > SARAM PAGE 1 .bss : { } > SARAM PAGE 1 .stack : { } > SARAM PAGE 1 .sysmem : { } > SARAM PAGE 1 } } Example: [/ size] [size= 4] MEMORY { PAGE 0: VECS: origin = 00000h, length = 00040h LOW: origin = 00040h, length = 03FC0h SARAM: origin = 04000h, length = 00800h B0: origin = 0FF00h, length = 00100h PAGE 1: B0: origin = 00200h, length = 00100h B1: origin = 00300h, length = 00100h[ /size] B2: origin = 00060h, length = 00020h SARAM: origin = 08000h, length = 00800h } SECTIONS[/ size] { .text : { } > LOW PAGE 0 .cinit : { } > LOW PAGE 0 .switch : { } > LOW PAGE 0 .const : { } > SARAM PAGE 1 .data : { } > SARAM PAGE 1 .bss : { } > SARAM PAGE 1 .stack : { } > SARAM PAGE 1 .sysmem: { } > SARAM PAGE 1 } } Example: [/ size] [size= 4] MEMORY { PAGE 0: VECS: origin = 00000h, length = 00040h [size= 4]LOW: origin = 00040h, length = 03FC0h SARAM: origin = 04000h, length = 00800h B0: origin = 0FF00h, length = 00100h PAGE 1: B0: origin = 00200h, length = 00100h B1: origin = 00300h, length = 00100h[ /size] B2: origin = 00060h, length = 00020h SARAM: origin = 08000h, length = 00800h } [ size=4] SECTIONS { .text : { } > LOW PAGE 0 [size=4 ].cinit : { } > LOW PAGE 0 .switch : { } > LOW PAGE 0 .const : { } > SARAM PAGE 1 .data : { } > SARAM PAGE 1 .bss : { } > SARAM PAGE 1 .stack : { } > SARAM PAGE 1 .sysmem : { } > SARAM PAGE 1 }