【发布时间】:2020-05-24 21:20:07
【问题描述】:
我正在用 GNU C 为一个爱好操作系统编写一个 FAT16 驱动程序,我有一个这样定义的结构:
struct directory_entry {
uint8_t name[11];
uint8_t attrib;
uint8_t name_case;
uint8_t created_decimal;
uint16_t created_time;
uint16_t created_date;
uint16_t accessed_date;
uint16_t ignore;
uint16_t modified_time;
uint16_t modified_date;
uint16_t first_cluster;
uint32_t length;
} __attribute__ ((packed));
我的印象是name 将与整个结构在同一个地址,而attrib 之后将是 11 个字节。事实上,(void *)e.name - (void *)&e 是 0,(void *)&e.attrib - (void *)&e 是 11,其中 e 的类型是 struct directory_entry。
在我的内核中,一个指向e 的空指针被传递给一个从磁盘读取其内容的函数。在此函数之后,*(uint8_t *)&e 为 80,*((uint8_t *)&e + 11 为 8,正如磁盘上所预期的那样。但是,e.name[0] 和 e.attrib 都是 0。
这里给出了什么?我是否误解了__attribute__ ((packed)) 的工作原理?具有相同属性的其他结构按照我对内核其他部分的期望工作。如果需要,我可以发布完整来源的链接。
编辑:完整源代码位于this gitlab repository 的stack-overflow 分支上。相关部分是 src/kernel/main.c 的第 34 到 52 行。当我检查*(uint8_t *)&e 和*((uint8_t *)&e + 11) 时,我确信数据填充正确。当我运行它时,该部分输出以下内容:
(void *)e.name - *(void *)&e
=> 0
*(uint8_t *)&e
=> 80
e.name[0]
=> 0
(void *)&e.attrib - (void *)&e
=> 11
*((uint8_t *)&e + 11)
=> 8
e.attrib
=> 0
我很困惑为什么e.name[0] 会与*(uint8_t *)&e 不同。
编辑2:我用objdump反汇编了这部分,看看编译后的代码有什么不同,但现在我更加困惑了。
u8_dec(*(uint8_t *)&e, nbuf); 和 u8_dec(e.name[0], nbuf); 都编译为:(cmets mine)
lea eax, [ebp - 0x30] ;loads address of e from stack into eax
movzx eax, byte [eax] ;loads byte pointed to by eax into eax, zero-extending
movzx eax, al ;not sure why this is here, as it's already zero-extended
sub esp, 0x8
push 0x31ce0 ;nbuf
push eax ;the byte we loaded
call 0x3162f ;u8_dec
add esp, 0x10
正如预期的那样,这会传入结构的第一个字节。我确定u8_dec 不会修改 e,因为它的第一个参数是按值传递的,而不是按引用传递的。 nbuf 是在文件范围内声明的数组,而 e 在函数范围内声明,所以它们不是重叠或任何东西。也许u8_dec 没有做好它的工作?这是它的来源:
void u8_dec(uint8_t n, uint8_t *b) {
if (!n) {
*(uint16_t *)b = '0';
return;
}
bool zero = false;
for (uint32_t m = 100; m; m /= 10) {
uint8_t d = (n / m) % 10;
if (zero)
*(b++) = d + '0';
else if (d) {
zero = true;
*(b++) = d + '0';
}
}
*b = 0;
}
现在很清楚,打包结构确实可以按照我的想法工作,但我仍然不确定是什么导致了问题。我将相同的值传递给应该是确定性的函数,但在不同的调用中我得到不同的结果。
【问题讨论】:
-
请出示代码。请创建一个minimal reproducible example
-
(void *)&e.attrib - (void *)&e不是正确的 C 代码。您不能使用void *指针进行指针运算。 -
@AndrewHenle:您可以使用 gcc,并且该问题已标记为 gcc 并使用其他 gcc 扩展...
-
你对
__attribute__((packed))的理解是正确的,所以你的bug很可能是别的。 -
既然您有了答案,请考虑将其写为答案。 StackOverflow 不是您通过编辑标题来标记已解决问题的论坛。你写一个答案并标记它。
标签: c gcc freestanding