当前位置: 首页 > news >正文

对齐与非对齐访问

对齐与非对齐访问

什么是非对齐访问

在机器指令层面,当尝试从不能被 N 整除 (addr % N != 0) 的起始地址读取 N 字节的数据时即发生了非对齐内存访问。举例而言,从地址 0x10004 读取 4 字节是可以的,然而从地址 0x10005 读取 4 字节数据将会是一个非对齐内存访问。这里 N 就是数据的自然对齐值

编译器非对齐访问支持

  • 使能非对齐访问 -munligned-access
  • 禁止非对齐访问 -mno-unligned-access

对于 ARM 架构,默认情况下 pre-ARMv6ARMv6-M 及以 ARMv8-M为基线架构的系列不支持非对齐访问,其他所有系列默认都是支持非对齐访问的。

对于 x86 架构,不支持 -mno-unligned-access-munligned-access 编译选项,非对对齐访问暂未研究

对于 RISC-V 架构,暂未研究

GCC 文档对于该编译选项的原文描述

Enables (or disables) reading and writing of 16- and 32- bit values from addresses that are not 16- or 32- bit aligned. By default unaligned access is disabled for all pre-ARMv6, all ARMv6-M and for ARMv8-M Baseline architectures, and enabled for all other architectures. If unaligned access is not enabled then words in packed data structures are accessed a byte at a time. The ARM attribute Tag_CPU_unaligned_access is set in the generated object file to either true or false, depending upon the setting of this option. If unaligned access is enabled then the preprocessor symbol __ARM_FEATURE_UNALIGNED is also defined

会产生非对齐访问的场景

指针强转

C 语言中类型强转的支持导致可能出现非对齐的访问出现。例如:

#include <stdio.h>int main(int argc, char argv[])
{printf("tyustli\r\n");char test_buf1[] = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10};printf("%p\r\n", &test_buf1[0]); // 0x7ffe63b95aaeprintf("%p\r\n", &test_buf1[1]); // 0x7ffe63b95aafprintf("%p\r\n", &test_buf1[2]); // 0x7ffe63b95ab0printf("%p\r\n", &test_buf1[3]); // 0x7ffe63b95ab1int value = *(int *)&test_buf1[3]; // 此时按照 int 去访问 0x7ffe63b95ab1 地址,就是非对齐的printf("0x%x\r\n", value);return 0;
}

指针的强制类型转换要小心,短类型的数据到长类型的转换要特别注意

结构体成员

如果代码中使用了 __attribute__((packed)) 定义的结构体,会出现结构体成员是非对齐的,此时如果没有使能非对齐访问会触发 abort 异常

struct test_struct_pack {short       test_value1;int         test_value2;char        test_value3;
} __attribute__((packed));int main()
{struct test_struct_pack test_pack;int val = test_pack.test_value2;return val;
}

参考链接

  • https://blog.csdn.net/FJDJFKDJFKDJFKD/article/details/108730380
  • http://www.360doc.com/content/22/0730/20/38701044_1041998980.shtml
  • https://club.rt-thread.org/ask/article/e179941c3f5c1955.html
http://www.lryc.cn/news/9903.html

相关文章:

  • 基于感知动作循环的层次推理用于视觉问答
  • python中的.nc文件处理 | 05 NetCDF数据的进一步分析
  • GGX发布全新路线图,揭示具备 Layer0 特性且可编程的跨链基建生态
  • taro+vue3 搭建一套框架,适用于微信小程序和H5
  • C++:模板初阶(泛型编程、函数模板、类模板)
  • 把数组排成最小的数 AcWing(JAVA)
  • 4.3 PBR
  • hmac — 加密消息签名和验证
  • AWS攻略——使用ACL限制访问
  • 【已解决】关于 luckysheet 设置纯文本,解决日期格式回显错误的办法
  • Jackson
  • 字节软件测试岗:惨不忍睹的三面,幸好做足了准备,月薪19k,已拿offer
  • vue使用axios发送post请求携带json body参数,后端使用@RequestBody进行接收
  • 【python百炼成魔】python之列表详解
  • 如何学习 Web3
  • 大数据框架之Hadoop:MapReduce(一)MapReduce概述
  • 一文搞定python语法进阶
  • 2019蓝桥杯真题数列求值(填空题) C语言/C++
  • spring中@Autowire和@Resource的区别在哪里?
  • 算法训练营DAY54|583. 两个字符串的删除操作、72. 编辑距离
  • 【Ctfshow_Web】信息收集和爆破
  • 基于机器学习的推荐算法研究与实现
  • (二十四)ATP应用测试平台——springboot集成fastdfs上传与下载功能
  • linux好用命令+vs快捷键
  • Git 构建分布式版本控制系统
  • Day891.一主多从的切换正确性 -MySQL实战
  • 【论文笔记】图像修复Learning Joint Spatial-Temporal Transformations for Video Inpainting
  • 代码随想录算法训练营第二天 | 977.有序数组的平方 、209.长度最小的子数组 、59.螺旋矩阵II、总结
  • Python pickle模块:实现Python对象的持久化存储
  • 【C++】C/C++内存管理