【问题标题】:Detect C++ inline symbols in COFF object file检测 COFF 目标文件中的 C++ 内联符号
【发布时间】:2020-07-05 20:52:49
【问题描述】:

我想生成一个 .def 文件,用于使用 MSVC 编译器构建 DLL。我使用 LLVM 搜索已编译的 .obj 文件以查找 C++ 函数或变量的所有定义。我想区分普通定义和内联定义,只导出DLL中的普通定义。我以这个简单的 .cpp 文件为例:

test.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)。对于std::string test()std::exception::exception(char const*, int),COFF 符号类是IMAGE_SYM_CLASS_EXTERNAL。有没有办法区分这些功能?

【问题讨论】:

    标签: c++ coff


    【解决方案1】:

    我自己找到了解决方案,信息包含在COMDAT节定义中,见https://docs.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 ...
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-10-01
      • 1970-01-01
      • 2014-11-08
      • 2021-09-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多