如何安全地将一组C函数实现为自初始化库?

时间:2022-11-28 09:35:19

I have never been able to find any clear, concise and complete explanations on the creation and use of libraries in C; both dynamic/shared and static.

我从来没有找到关于在C中创建和使用库的任何清晰,简洁和完整的解释;动态/共享和静态。

For a couple of reasons, I would like to implement some of my C header files for a project I am working on as libraries. Each library may have a global structure acting as a namespace for static functions in the headers, and an entry function to initialize this structure.

出于几个原因,我想为我正在处理的项目实现一些我的C头文件。每个库可以具有作为标题中的静态函数的命名空间的全局结构,以及用于初始化该结构的入口函数。

struct mylibrary {
    int (*function1)(void);
    int (*function2)(void);
} mylibrary;

static int my_library_function_1 (void) {
    return 1;
}

static int my_library_function_2 (void) {
    return 2;
}

void entry_mylibrary (void) {
    mylibrary.function1 = my_library_function_1;
    mylibrary.function2 = my_library_function_2;
}

As I understand it, this should work without a problem with a static library. All I have to do is link the library and call the function entry_mylibrary from elsewhere in my program. However, I would like my libraries to be self-initializing; that is, I want the entry function to be called when the code is loaded. As I understand it, that is the domain of shared libraries, which can declare entry points to be executed when the library is loaded.

据我了解,这应该没有静态库的问题。我所要做的就是链接库并从我的程序中的其他地方调用函数entry_mylibrary。但是,我希望我的图书馆能够自我初始化;也就是说,我希望在加载代码时调用entry函数。据我所知,这是共享库的领域,它可以声明加载库时要执行的入口点。

So why don't I simply create dynamic libraries?

那我为什么不简单地创建动态库呢?

Well, I am worried about application security. If I am just linking a library at runtime, I don't see what is to prevent some end user from swapping my library for their own. Could I not create a new library declaring the same variables as the old one, and swap the definitions of these variables for my own? I don't see why the executable would care.

好吧,我担心应用程序安全性。如果我只是在运行时链接一个库,我不知道是什么阻止一些最终用户交换我自己的库。难道我不能创建一个新的库来声明与旧的变量相同的变量,并将这些变量的定义交换为我自己的变量吗?我不明白为什么可执行文件会关心。

I suppose then that my questions are:

我想那时我的问题是:

  • Is there some obscure method to self-initializing a static library?
  • 是否有一些模糊的方法来自我初始化静态库?
  • How can I verify that the (shared) library I load is indeed MY library?
  • 如何验证我加载的(共享)库确实是我的库?

I am/will be working cross platform. 64 bit Windows and Linux. Apple products are to-be-determined, but not a priority.

我/将在跨平台工作。 64位Windows和Linux。 Apple产品是待定的,但不是优先考虑的事项。

I am using Visual Studio 2017 Community on Windows 10. I will be using GCC for linux. I will be implementing the same libraries on all platforms with platform compilers, e.g. VS 2017 on Windows and GCC on linux; I won't be cross-compiling platform-specific code.

我在Windows 10上使用Visual Studio 2017社区。我将使用GCC for linux。我将使用平台编译器在所有平台上实现相同的库,例如Windows上的VS 2017和Linux上的GCC;我不会交叉编译特定于平台的代码。

1 个解决方案

#1


1  

Is there some obscure method to self-initializing a static library?

是否有一些模糊的方法来自我初始化静态库?

With GCC it is not obscure: just use __attribute__((constructor)). With MSVC it's a bit harder, but still possible: __attribute__((constructor)) equivalent in VC?

使用GCC并不模糊:只需使用__attribute __((构造函数))。使用MSVC它有点困难,但仍然可能:VC中的__attribute __((构造函数))等效?

How can I verify that the (shared) library I load is indeed MY library?

如何验证我加载的(共享)库确实是我的库?

With the above you won't need to use a shared library.

有了上述内容,您将无需使用共享库。

#1


1  

Is there some obscure method to self-initializing a static library?

是否有一些模糊的方法来自我初始化静态库?

With GCC it is not obscure: just use __attribute__((constructor)). With MSVC it's a bit harder, but still possible: __attribute__((constructor)) equivalent in VC?

使用GCC并不模糊:只需使用__attribute __((构造函数))。使用MSVC它有点困难,但仍然可能:VC中的__attribute __((构造函数))等效?

How can I verify that the (shared) library I load is indeed MY library?

如何验证我加载的(共享)库确实是我的库?

With the above you won't need to use a shared library.

有了上述内容,您将无需使用共享库。