Solaris 10 SPARC
Sun Studio C编译器 12.3
在 SPARC64 机器上,如果您访问的变量在相关的 4 或 8 字节边界上未正确对齐,您将获得核心转储。这需要编码人员跳过几个环节来应对这个要求,(但也让你编写可移植的代码)。
如果我们有一个模拟网络协议头的C结构(即这 16 位是端口,这 8 位是标志等),如果我们使用对齐指令来适应 SPARC64 处理器,这是否仍然保留字节映射,否则一切都会破裂。是否有逻辑从结构的布局中隐藏字节存储的实现。
typedef struct TCPHdr_
{
uint16_t th_sport; /**< source port */
uint16_t th_dport; /**< destination port */
uint32_t th_seq; /**< sequence number */
uint32_t th_ack; /**< acknowledgement number */
uint8_t th_offx2; /**< offset and reserved */
uint8_t th_flags; /**< pkt flags */
uint16_t th_win; /**< pkt window */
uint16_t th_sum; /**< checksum */
uint16_t th_urp; /**< urgent pointer */
} TCPHdr;
像这样对齐:
typedef struct TCPHdr_
{
uint16_t th_sport __attribute__((aligned(8))); /**< source port */
uint16_t th_dport __attribute__((aligned(8))); /**< destination port */
uint32_t th_seq __attribute__((aligned(8))); /**< sequence number */
uint32_t th_ack __attribute__((aligned(8))); /**< acknowledgement number */
uint8_t th_offx2 __attribute__((aligned(8))); /**< offset and reserved */
uint8_t th_flags __attribute__((aligned(8))); /**< pkt flags */
uint16_t th_win __attribute__((aligned(8))); /**< pkt window */
uint16_t th_sum __attribute__((aligned(8))); /**< checksum */
uint16_t th_urp __attribute__((aligned(8))); /**< urgent pointer */
} TCPHdr;
大多数情况下,它是关于这样的代码的查询:
SET_PKT_LEN(p, sizeof(IPV6Hdr) + sizeof(TCPHdr) + payload_len);
和
p1->tcph = (TCPHdr *)raw_tcp;
其中原始字节被转换为结构或sizeof()
测试结构的大小。它还能工作还是新结构不能映射网络字节?