检测 COFF 对象文件中C++内联符号
Detect C++ inline symbols in COFF object file
我想生成一个.def文件,用于使用MSVC编译器构建DLL。我使用 LLVM 搜索编译的 .obj 文件,以查找C++函数或变量的所有定义。我想区分普通定义和内联定义,以便仅导出 DLL 中的普通定义。我用这个简单的.cpp文件作为例子:
测试.cpp:
#include <string>
static std::string static_test() {
return "test.cpp";
}
std::string test() { return static_test(); }
编译器生成包含标准库定义的多个函数的目标文件test.cpp.obj
。其中一个函数是内联构造函数public: __cdecl std::exception::exception(char const *const, int)
。COFF 符号类对于std::string test()
和std::exception::exception(char const*, int)
都IMAGE_SYM_CLASS_EXTERNAL
。有没有办法分解这些功能?
我自己找到了一个解决方案,该信息包含在 COMDAT 部分定义中,请参阅 https://learn.microsoft.com/en-us/windows/win32/debug/pe-format#comdat-sections-object-only。
这是我使用 LLVM 检测定义的代码:
void process_obj(const llvm::object::COFFObjectFile &object) {
std::vector<bool> section_contains_definition(object.getNumberOfSections() + 1);// sections are 1-based, +1 to avoid index arithm
for (const auto &sym : object.symbols()) {
const auto &coff_symbol = object.getCOFFSymbol(sym);
if (coff_symbol.isSectionDefinition()) {
const llvm::object::coff_section *section = nullptr;
uint32_t section_number = coff_symbol.getSectionNumber();
object.getSection(section_number, section);
if (!section)
continue;
// this is true for some sections containing global variables
if (!(section->Characteristics & llvm::COFF::IMAGE_SCN_LNK_COMDAT)) {
section_contains_definition[section_number] = true;
} else {
auto *def = coff_symbol.getSectionDefinition();
section_contains_definition[section_number] =
def->Selection == llvm::COFF::IMAGE_COMDAT_SELECT_NODUPLICATES;
}
}
}
for (const auto &sym : object.symbols()) {
const auto &coff_symbol = object.getCOFFSymbol(sym);
if (!coff_symbol.isExternal() || coff_symbol.isAnyUndefined() || !section_contains_definition[coff_symbol.getSectionNumber()])
continue;
... this coff_symbol is a definition ...
}
}
相关文章:
- 在C++中,将大的无符号浮点数四舍五入为整数的最佳方法是什么
- 有符号的int和int-有没有一种方法可以在C++中区分它们
- 将无符号char*转换为std::istream*C++
- 如何在C++中将一个无符号的 int 转换为两个无符号的短裤?
- vscode g++链路故障:体系结构x86_64的未定义符号
- 从矢量<无符号字符>转换为字符* 包括垃圾数据
- 如何理解将半精度指针转换为无符号长指针和相关的内存对齐
- Visual studio代码重构似乎不起作用(例如,重命名符号-f2)
- 使用gcc从静态链接的文件中查找可选符号
- C++中无符号字符溢出
- 使用无符号字符数组有效存储内存
- C++:Application.cpp中抛出了未解析的外部符号(解决方案在问题的末尾,供未来的读者参考)
- VC++本机单元测试,找不到调试符号
- 为什么我必须在C++中添加一个赋值符号来声明一个数组
- 检查TCHAR数组输入是否为带符号整数C++
- 用符号版本替换对函数的所有调用
- 未解析的外部符号_MsiLocateComponentW@12.
- 如何打印boost多精度128位无符号整数
- C++模板函数,用于比较任何无符号整数和有符号整数
- 在 Mac 上使用 CMAKE 将 FFTW 和 FFTWPP 链接到项目中时未定义的符号