1

I would like to generate a .def file for building a DLL with the MSVC compiler. I used LLVM to search through the compiled .obj files to find all definitions of C++ functions or variables. I would like to distinguish between normal definitions and inline definitions to only export the normal definitions in the DLL. I used this simple .cpp file as an example:

test.cpp:

#include <string>

static std::string static_test() {
    return "test.cpp";
}

std::string test() { return static_test(); }

The compiler generates the object file test.cpp.obj that contain several functions defined by the standard library. One of these functions is the inline constructor public: __cdecl std::exception::exception(char const *const, int). The COFF symbol class is IMAGE_SYM_CLASS_EXTERNAL for both std::string test() and std::exception::exception(char const*, int). Is there a way to distiguish these functions?

tly
  • 1,202
  • 14
  • 17

1 Answers1

0

I found a solution myself, the information is contained in the COMDAT section definition, see https://learn.microsoft.com/en-us/windows/win32/debug/pe-format#comdat-sections-object-only.

This is my code using LLVM to detect definitions:

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 ...
    }
}
tly
  • 1,202
  • 14
  • 17