目录

attribute-packed 编译属性-字节对齐

目录

简介

对于嵌入式底层二进制通讯数据,往往需要取消处理器默认长度的数据对齐方式(例如:4 字节对齐),而是取用 1 字节对齐方式。对于这类需求,GCC 的编译属性__attribute__((packed))为我们提供了解决方案。另外:#pragma pack(push,1) #pragma pack(pop)则是一样效果的另一套方案。
更多的 attribute 编译属性
attribute-section 编译属性-数据拼接
attribute-aligned 编译属性-地址对齐
attribute-packed 编译属性-字节对齐
attribute-weak 编译属性-弱符号
attribute-un/used 编译属性-未用警告
attribute-at 编译属性-地址指定

使用

1、作用对象

__attribute__((packed))作用对象:
作用于结构体中每个成员,其所有变量数据都是按照单字节方式对齐。

2、应用情景

在我们单片机的通讯中,一般都是这样定义通讯协议:第一字节为帧头,第二字节为数据长度,第三字节是命令……。对于很多刚入行的工程师基本上都是这样读写数据:使用单字节数组方式读写数据(例如:d[1] = 5)。这样的写法缺点很多,一是这种偏移量难理解其意义不利用阅读且容易出错,二是如果中间要插入参数则其后面所有偏移量都要修改,三是对于整型等这类数据访问很不方便(例如:u16 = (d[11]<<8) | d[10])。其实我们可以将这组数据进行格式化,只需通过结构体定义这组数据,并且使用特定扩展语法强制结构体数据以单字节方式对齐,之后就可以非常方便地使用结构体方式对数据进行读写(例如:d.cmd = 1)。

3、编程语法
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include <stdio.h>
#include <stdint.h>
int main(int arg1, char *arg2[])
{
	//----------------------------------------------------------------------------------------------------
	//方式1、__attribute__((packed))
	//----------------------------------------------------------------------------------------------------
	typedef struct data1_
	{
		uint8_t  head;        //帧头
		uint8_t  len;         //数据长度
		uint8_t  cmd;         //命令
		uint32_t d1;          //数据1
		uint16_t d2;          //数据2
		uint8_t  checksum;    //校验和
	}__attribute__((packed)) data1_t;

	data1_t dd1; ////////////////////
	dd1.head     = 1;         //1字节
	dd1.len      = 7;         //1字节
	dd1.cmd      = 123;       //1字节
	dd1.d1       = 987654321; //4字节
	dd1.d2       = 65535;     //2字节
	dd1.checksum = 0;         //1字节

	printf("dd1.d1=%u\n", dd1.d1);           //dd1.d1=987654321
	printf("dd1.d2=%u\n", dd1.d2);           //dd1.d2=65535
	printf("sizeof(dd1)=%u\n", sizeof(dd1)); //sizeof(dd1)=10
	printf("----------------\n");            //----------------

	//----------------------------------------------------------------------------------------------------
	//方式2、#pragma pack(push,1)  #pragma pack(pop)
	//----------------------------------------------------------------------------------------------------
	#pragma pack(push,1)      //(push)与(pop)要配对, 可以嵌套; 1->1字节对齐, 其可 1,2,4,8,16
	typedef struct data2_     //#pragma pack(push,1) 相当于两条语句:#pragma pack(push)  #pragma pack(1)
	{
		uint8_t  head;        //帧头
		uint8_t  len;         //数据长度
		uint8_t  cmd;         //命令
		uint32_t d1;          //数据1
		uint16_t d2;          //数据2
		uint8_t  checksum;    //校验和
	}data2_t;
	#pragma pack(pop)

	data2_t dd2; ////////////////////
	dd2.head     = 1;         //1字节
	dd2.len      = 7;         //1字节
	dd2.cmd      = 123;       //1字节
	dd2.d1       = 987654321; //4字节
	dd2.d2       = 65535;     //2字节
	dd2.checksum = 0;         //1字节

	printf("dd2.d1=%u\n", dd2.d1);           //dd2.d1=987654321
	printf("dd2.d2=%u\n", dd2.d2);           //dd2.d2=65535
	printf("sizeof(dd2)=%u\n", sizeof(dd2)); //sizeof(dd2)=10
	printf("----------------\n");            //----------------
}

4、语法总结

一、关于__attribute__(())的参数名称,为了防止与其它对象出现同名影响,强烈建议在参数的前后都加上__两个下划线。

1
2
3
4
5
6
7
8
__attribute__((section(x))) 改为  __attribute__((__section__(x)))
__attribute__((at(a)))      改为  __attribute__((__at__(a)))
__attribute__((packed))     改为  __attribute__((__packed__))
__attribute__((aligned(n))) 改为  __attribute__((__aligned__(n)))
__attribute__((unused))     改为  __attribute__((__unused__))
__attribute__((used))       改为  __attribute__((__used__))
__attribute__((weak))       改为  __attribute__((__weak__))

二、关于__attribute__(())语句书写位置总体原则:书写到修饰对象名称的后面(★修饰其左边的单元体(非每个元素),放到最前面即是修饰整体★),但考虑要跨编译平台使用,强烈建议使用宏定义并且按下面规则使用。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
//【总体原则】+++++++++++++++++++
#define O2O_SECTION(x)   __attribute__((__section__(x)))    //数据拼接 (对象名称后明声明)
#define O2O_AT(a)        __attribute__((__at__(a)))         //地址指定 (对象名称后明声明)
#define O2O_PACKED       __attribute__((__packed__))        //字节对齐 (对象名称后明声明,强烈建议改用 #pragma pack(push, 1) ... #pragma pack(pop) 的兼容性更好)
#define O2O_ALIGN(n)     __attribute__((__aligned__(n)))    //地址对齐 (对象整体最前声明)
#define O2O_UNUSED       __attribute__((__unused__))        //未用不警告(对象整体最前声明)
#define O2O_USED         __attribute__((__used__))          //未用不优化(对象整体最前声明)
#define O2O_WEAK         __attribute__((__weak__))          //弱化对象 (对象整体最前声明)
#define O2O_INLINE       static __inline                    //内联函数 (对象整体最前声明,c/h文件中直接编写函数(体),不能外部声明)

三、关于__attribute__(())__aligned__(n)参数对【结构体类型】修饰的特殊表现(只是唯一的特殊个案):

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
typedef struct obj_1_
{
	uint16_t a;
	uint8_t  b;

}obj_1_t __attribute__((__aligned__(64))); //1.用于[单体]结构体类型的【起始地址】

typedef struct obj_n_
{
	uint16_t a;
	uint8_t  b;

}__attribute__((__aligned__(64))) obj_n_t; //2.用于结构体[组员]类型的【起始地址】和【大小】,也可用于单体结构体!备注:是组员非成员!
                //                                                -┬-
obj_1_t aaaaaa; //1.影响[单体结构体]的起始地址对齐,不能用于数组                      ├→ ●只是唯一的特殊个案●
obj_n_t bbb[6]; //2.影响结构体[每个组员]的起始地址对齐和大小限制----------------------┘
                //3.影响[数组整体]起始地址,但不影响[其它组员]起始地址和大小!
__attribute__((__aligned__(64))) struct obj_1_ ccc[8]; //←┤
__attribute__((__aligned__(64))) struct obj_n_ ddd[8]; //←┘

四、关于__attribute__(())__section__(x)参数被【编译器】与【链接器】优化的问题:

【section】修饰的数据段会被【编译器】和【链接器】这两道关卡优化掉,需要增加特别语句加以防止!【used、unused】只针对【编译器】而言,但对于【链接器】无效,也就是说链接器的【-Wl,–gc-sections】参数还是会优化掉没使用的段,除非在【链接脚本】中使用【KEEP】语句特别指出的段才不会被优化掉!

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
//1.变量声明
__attribute__((__used__)) const uint8_t aaa __attribute__((__section__(".init.001"))); //增加__used__防止被[编译器]优化
__attribute__((__used__)) const uint8_t bbb __attribute__((__section__(".init.002"))); //增加__used__防止被[编译器]优化

//2.编译链接
gcc -Wl,--gc-sections -o hello hello.c /* -Wl,--gc-sections 会强制优化掉没使用的__section__(函数段/数据段)*/

//3.链接脚本
.text :
{
  .............
  
  . = ALIGN(4);
  KEEP(*(SORT(.init.*)))               /* 使用 KEEP(*(SORT())) 防止被[链接器]优化 */

  . = ALIGN(4);
  _etext = .;
} >FLASH