Linker 如何在MacOSX中获取指向二进制部分的指针?

Linker 如何在MacOSX中获取指向二进制部分的指针?,linker,ld,Linker,Ld,我正在编写一些代码,将一些数据结构存储在一个特殊命名的二进制部分中。这些都是同一结构的实例,它们分散在许多C文件中,彼此不在范围内。通过将它们全部放在命名的部分中,我可以对它们进行迭代 这与GCC和gnuld完美配合。由于缺少\uuuuuuuuu mysection和\uuuuuuu mysection符号,在Mac OS X上失败。我猜llvm ld不够聪明,无法自动提供它们 在GCC和GNU ld中,我使用\uuuuu属性(section(…)加上一些特殊命名的外部指针,这些指针由链接器神奇

我正在编写一些代码,将一些数据结构存储在一个特殊命名的二进制部分中。这些都是同一结构的实例,它们分散在许多C文件中,彼此不在范围内。通过将它们全部放在命名的部分中,我可以对它们进行迭代

这与GCC和gnuld完美配合。由于缺少
\uuuuuuuuu mysection
\uuuuuuu mysection
符号,在Mac OS X上失败。我猜llvm ld不够聪明,无法自动提供它们

在GCC和GNU ld中,我使用
\uuuuu属性(section(…)
加上一些特殊命名的外部指针,这些指针由链接器神奇地填充。下面是一个简单的示例:

#include <stdio.h>

extern int __start___mysection[];
extern int __stop___mysection[];

static int x __attribute__((section("__mysection"))) = 4;
static int y __attribute__((section("__mysection"))) = 10;
static int z __attribute__((section("__mysection"))) = 22;

#define SECTION_SIZE(sect) \
    ((size_t)((__stop_##sect - __start_##sect)))

int main(void)
{
    size_t sz = SECTION_SIZE(__mysection);
    int i;

    printf("Section size is %u\n", sz);

    for (i=0; i < sz; i++) {
        printf("%d\n", __start___mysection[i]);
    }

    return 0;
}

关于MSVC,这里也有类似的问题:

使用马赫数信息:

#include <mach-o/getsect.h>

char *secstart;
unsigned long secsize;
secstart = getsectdata("__SEGMENT", "__section", &secsize);

更多信息:

您可以让达尔文链接器为您执行此操作

#include <stdio.h>

extern int start_mysection __asm("section$start$__DATA$__mysection");
extern int stop_mysection  __asm("section$end$__DATA$__mysection");

// If you don't reference x, y and z explicitly, they'll be dead-stripped.
// Prevent that with the "used" attribute.
static int x __attribute__((used,section("__DATA,__mysection"))) = 4;
static int y __attribute__((used,section("__DATA,__mysection"))) = 10;
static int z __attribute__((used,section("__DATA,__mysection"))) = 22;

int main(void)
{
    long sz = &stop_mysection - &start_mysection;
    long i;

    printf("Section size is %ld\n", sz);

    for (i=0; i < sz; ++i) {
        printf("%d\n", (&start_mysection)[i]);
    }

    return 0;
}
#包括
extern int start_mysection(第$start$节数据$节);
extern int stop\u mysection\uuuu asm(“section$end$\uuuuu DATA$\uuuu mysection”);
//如果你不明确地引用x,y和z,它们将被完全剥离。
//使用“used”属性防止这种情况。
静态整数x uuu属性uuu((已使用,节(“uuu数据,uuu mysection”))=4;
静态int y uuu属性uuu((已使用,节(“uu数据,uu mysection”))=10;
静态int z uuu属性uuu((已使用,节(“uuu数据,uuu mysection”))=22;
内部主(空)
{
long sz=&stop\u mysection-&start\u mysection;
龙我;
printf(“节大小为%ld\n”,sz);
对于(i=0;i
int x __attribute__((section("__SEGMENT,__section"))) = 123;
#include <stdio.h>

extern int start_mysection __asm("section$start$__DATA$__mysection");
extern int stop_mysection  __asm("section$end$__DATA$__mysection");

// If you don't reference x, y and z explicitly, they'll be dead-stripped.
// Prevent that with the "used" attribute.
static int x __attribute__((used,section("__DATA,__mysection"))) = 4;
static int y __attribute__((used,section("__DATA,__mysection"))) = 10;
static int z __attribute__((used,section("__DATA,__mysection"))) = 22;

int main(void)
{
    long sz = &stop_mysection - &start_mysection;
    long i;

    printf("Section size is %ld\n", sz);

    for (i=0; i < sz; ++i) {
        printf("%d\n", (&start_mysection)[i]);
    }

    return 0;
}