gcc c 编译器中的 cswtch 生成
cswtch generation in gcc c compiler
我对 gcc 编译器中的目标代码生成有疑问。
在我的程序中,objdump 显示生成了 CSWTCH
个部分。
您能否解释一下 C 代码中的哪些条件需要 CSWTCH
节
生成?
CSWTCH
部分的分配输出部分是否在 .rodata
.
中
在什么情况下,small rodata 将被指定为 CSWTCH
的输出部分。
答案是 (1) 没有这样的标准:这只是一种编译器生成值的技术 table; (2) 这取决于编译器、汇编器和链接器; (3) 这取决于编译器、汇编器和链接器。
GCC(至少是 gcc 版本 5)在 x86 上发出 .section
和 .type
指令:
$ cat cswitch.c
int sw_2 (char x)
{
switch(x)
{
case '0': return -1;
case '1': return 2;
case '2': return 3;
case '3': return 5;
case '4': return 7;
case '5': return 11;
case '6': return 13;
case '7': return 17;
case '8': return 19;
case '9': return 23;
case 'a':return 29;
case 'A':return 29;
}
return -1;
}
$ gcc -Os -S cswitch.c
$ cat cswitch.s
.file "cswitch.c"
[mass snippage]
.section .rodata
.align 32
.type CSWTCH.1, @object
.size CSWTCH.1, 49
CSWTCH.1:
(我发现需要 -Os
开关才能在 x86 上生成查找 table。使用 -O
,我得到一个更典型的跳转 table。)
在这种情况下,.section
指令最终应用 table 并将其放入 .rodata
部分。但这只是一种系统的实现方式,对编译器没有硬性要求。
请注意,您可以进行 可能 的源转换,以使编译器在只读数据部分发出 table:
int sw_3(char x)
{
const char table['a' - '1'] = {
'1' - '1': 2,
'2' - '1': 3,
'3' - '1': 5,
/* ... fill in the remainder as needed */
};
if (x >= '1' && x <= 'A') return table[x - '1'];
return -1;
}
(此转换假设系统使用 ASCII 或 UTF-8 或类似的。)但是,即使在这里,编译器也可以生成任意机器代码,只要它产生适用的任何标准所需的结果。
我对 gcc 编译器中的目标代码生成有疑问。
在我的程序中,objdump 显示生成了 CSWTCH
个部分。
您能否解释一下 C 代码中的哪些条件需要
CSWTCH
节 生成?CSWTCH
部分的分配输出部分是否在.rodata
. 中
在什么情况下,small rodata 将被指定为
CSWTCH
的输出部分。
答案是 (1) 没有这样的标准:这只是一种编译器生成值的技术 table; (2) 这取决于编译器、汇编器和链接器; (3) 这取决于编译器、汇编器和链接器。
GCC(至少是 gcc 版本 5)在 x86 上发出 .section
和 .type
指令:
$ cat cswitch.c
int sw_2 (char x)
{
switch(x)
{
case '0': return -1;
case '1': return 2;
case '2': return 3;
case '3': return 5;
case '4': return 7;
case '5': return 11;
case '6': return 13;
case '7': return 17;
case '8': return 19;
case '9': return 23;
case 'a':return 29;
case 'A':return 29;
}
return -1;
}
$ gcc -Os -S cswitch.c
$ cat cswitch.s
.file "cswitch.c"
[mass snippage]
.section .rodata
.align 32
.type CSWTCH.1, @object
.size CSWTCH.1, 49
CSWTCH.1:
(我发现需要 -Os
开关才能在 x86 上生成查找 table。使用 -O
,我得到一个更典型的跳转 table。)
在这种情况下,.section
指令最终应用 table 并将其放入 .rodata
部分。但这只是一种系统的实现方式,对编译器没有硬性要求。
请注意,您可以进行 可能 的源转换,以使编译器在只读数据部分发出 table:
int sw_3(char x)
{
const char table['a' - '1'] = {
'1' - '1': 2,
'2' - '1': 3,
'3' - '1': 5,
/* ... fill in the remainder as needed */
};
if (x >= '1' && x <= 'A') return table[x - '1'];
return -1;
}
(此转换假设系统使用 ASCII 或 UTF-8 或类似的。)但是,即使在这里,编译器也可以生成任意机器代码,只要它产生适用的任何标准所需的结果。