生成正确的.DEF文件以导出非静态函数和GLOBALS [英] Generating correct .DEF files to export non-static functions AND GLOBALS

查看:119
本文介绍了生成正确的.DEF文件以导出非静态函数和GLOBALS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

接着是有关检测不良链接的问题遍历dll边界的全局变量,事实证明,我需要修改PostgreSQL项目使用的.DEF文件生成器工具,以便它为全局变量的.DEF条目正确发出DATA标记.

Following on from a question about detecting bad linkage to globals across dll boudaries, it turns out that I need to modify a .DEF file generator tool used by the PostgreSQL project so that it correctly emits DATA tags for .DEF entries for global variables.

我似乎找不到使用Microsoft工具的方法,以获取一个符号表列表,该符号表列出了区分全局变量和函数,并且包括未在其定义站点初始化的全局变量.

I can't seem to find a way, using Microsoft's tools, to get a symbol table listing that differentiates between global variables and functions, and that includes globals that aren't initialized at their definition site.

想法?

该工具在dumpbin /symbols输出上循环生成.DEF文件.与我习惯的nm不同,dumpbin /symbols似乎没有为每个符号发出一个条目来指示符号类型-函数,初始化变量,未初始化变量.它仅显示符号是否在本地定义.

The tool loops over dumpbin /symbols output to generate the .DEF file. Unlike nm, which I'm used to, dumpbin /symbols does not appear to emit an entry for each symbol to indicate the symbol type - function, initialized variable, uninitialized variable. It only shows whether the symbol is locally defined or not.

对于每个dumpbin输出行,以及.c文件中的相应定义,我们首先有一个初始化的全局变量:

With each dumpbin output line followed by the corresponding definition in the .c file, we have first an initialized global:

00B 00000000 SECT3  notype       External     | _DefaultXactIsoLevel
int         DefaultXactIsoLevel = XACT_READ_COMMITTED;

相对于具有非静态链接的函数:

vs a function with non-static linkage:

022 00000030 SECT5  notype ()    External     | _IsAbortedTransactionBlockState
bool IsAbortedTransactionBlockState(void) {...}

...并且为了获得额外的乐趣,未初始化的全局变量似乎显示为UNDEF,就像引用其他编译单元的符号一样,例如:

... and for bonus fun, un-initialized globals appear to be shown as UNDEF, just like references to symbols from other compilation units, e.g:

007 00000004 UNDEF  notype       External     | _XactIsoLevel
int         XactIsoLevel;

即使在编译过程中在标头中对此进行了预先声明(为了便于阅读,也已扩展了项目专用的宏指针):

even though this is pre-declared in the header during compilation (with project specific macro hand expanded for readability) as:

extern __declspec(dllexport) int XactIsoLevel;

所以...看来dumpbin输出的信息不足,无法生成正确的.DEF文件.

So... it looks like dumpbin output doesn't contain enough information to generate a correct .DEF file.

现在,gendefs.pl欣喜地吐出了一个.DEF文件,该文件忽略了未初始化的全局变量,并将其他所有内容声明为代码(通过在.DEF中未指定CONSTANTDATA).对于损坏的东西,效果非常好.

Right now gendefs.pl is merrily spitting out a .DEF file that omits globals that aren't initialized, and declares everything else as code (by failing to specify CONSTANT or DATA in the .DEF). For something so broken, it's worked remarkably well.

要生成正确的.DEF文件,我需要一种方法来确定哪些符号是变量.

To produce correct .DEF files, I need a way to determine which symbols are variables.

我看过使用cl.exe/Fm选项,但这只是链接器的/MAP选项的直通,当您仅生成目标文件而不是将其链接时,它什么也不做.

I looked at using cl.exe's /Fm option, but it's just a passthrough to the linker's /MAP option, and does nothing when you're just generating an object file, not linking it.

我可以使用符号转储工具来生成更多有用的信息,例如gcc的nm.exe,但它会增加工具的依赖关系,而且似乎很脆弱.

I could use a symbol dump tool that produces more useful information like gcc's nm.exe, but that adds extra tool dependencies and seems fragile.

在这一点上,我不能简单地用PGDLLIMPORT(项目使用的__declspec(dllimport)/__declspec(dllexport)宏)注释每个导出的函数,并停止使用DEF文件.

At this point I am not able to simply annotate every exported function with PGDLLIMPORT (the __declspec(dllimport) / __declspec(dllexport) macro used by the project) and stop using a DEF file.

即使我需要找到一种方法,当在暴露变量上省略PGDLLIMPORT时,也会导致明显的链接器错误.

Even if I could I need to find an approach that will cause clear linker errors when PGDLLIMPORT is omitted on an exposed variable.

所以. Windows链接器/编译器专家.有什么想法吗?

So. Windows linker/compiler experts. Any ideas?

推荐答案

好吧,我必须说我说微软的工具根本不使用符号类型字段是错的.

Well, I must say I was wrong saying that microsoft tools doesn't use symbol type field at all.

1). cl不会使用它来区分实际的类型信息,但是会存储您需要的信息:

1). cl doesn't use it to differentiate actual type info, but it stores information that you need:

  • 0x20表示功能
  • 0x00表示不是函数
  • 0x20 means function
  • 0x00 means not a function

PE/COFF规范,第p. 46-47.

PE/COFF specification, p. 46-47.

您可以在dumpbin的输出中的符号类型(在您的情况下为notype)之后搜索()的存在/不存在,以查找它是code还是data.

You may search for presence/abscence of () after symbol type (notype in your case) in dumpbin's output to find whether it is code or data.

2).另外,clobj文件中生成链接器的特殊部分,该部分包含export开关,用于/export:symbol[,type]形式的每个__declspec(dllexport)符号.

2). Also, cl generates in obj files special section for linker which include export switch for every __declspec(dllexport) symbol in the form /export:symbol[,type].

3).最后,由于修改,您可以指定"C ++"外部链接并获取符号的类型.

3). And last, you can specify 'C++' external linkage and get symbols' types because of mangling.

这篇关于生成正确的.DEF文件以导出非静态函数和GLOBALS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆