问题
- pfn_test 类型的 pfn, 长度使用++运算,为什么每次只加 1 ?
- func1,func2 加入到 section 中后,间隔变为 27 字节, 这个依据是什么,试了几个平台,这个值也不固定,如果要遍历 section 中的函数,这个 pfn++要如何写?
$cat main.c
#include <stdint.h>
#include <stdio.h>
typedef int (*pfn_test)(void);
#define UTEST __attribute__(( unused, section("mysection")))
int UTEST func1(void)
{
printf("func is %s\n", __func__);
}
static int UTEST func2(void)
{
printf("func is %s\n", __func__);
}
extern pfn_test __start_mysection;
extern pfn_test __stop_mysection;
int main(int argc, char *argv[])
{
printf("start sec %p\n", &__start_mysection);
printf("stop sec %p\n", &__stop_mysection);
printf("func1 %p\n", func1);
printf("func2 %p, %lu\n", func2, func2 - func1);
pfn_test pfn = &__start_mysection;
printf("size %lu\n", sizeof(pfn));
for (int i = 0; i <3; ++i) {
printf("addr %p\n", pfn++);
}
pfn = &__start_mysection;
for (int i = 0; i <2; ++i) {
pfn();
pfn += 27;
}
printf("done");
}
$ gcc main.c && ./a.out
start sec 0x4006a2
stop sec 0x4006d8
func1 0x4006a2
func2 0x4006bd, 27
size 8
addr 0x4006a2
addr 0x4006a3
addr 0x4006a4
func is func1
func is func2