| 120 | } |
| 121 | |
| 122 | std::vector<ExportedSymbol> PEParser::GetExports() const { |
| 123 | std::vector<ExportedSymbol> exports; |
| 124 | if (!HasExports()) |
| 125 | return exports; |
| 126 | auto dir = GetDataDirectory(IMAGE_DIRECTORY_ENTRY_EXPORT); |
| 127 | if (dir == nullptr || dir->Size == 0) |
| 128 | return exports; |
| 129 | |
| 130 | auto data = static_cast<IMAGE_EXPORT_DIRECTORY*>(GetAddress(dir->VirtualAddress)); |
| 131 | auto count = data->NumberOfFunctions; |
| 132 | exports.reserve(count); |
| 133 | |
| 134 | auto names = (PBYTE)(data->AddressOfNames != 0 ? GetAddress(data->AddressOfNames) : nullptr); |
| 135 | auto ordinals = (PBYTE)(data->AddressOfNameOrdinals != 0 ? GetAddress(data->AddressOfNameOrdinals) : nullptr); |
| 136 | auto functions = (PDWORD)GetAddress(data->AddressOfFunctions); |
| 137 | char undecorated[1 << 10]; |
| 138 | auto ordinalBase = data->Base; |
| 139 | |
| 140 | std::unordered_map<uint32_t, std::string> functionNamesMap; |
| 141 | for (uint32_t idx = 0; idx < data->NumberOfNames; idx++) { |
| 142 | uint16_t ordinal; |
| 143 | ordinal = *(USHORT*)(ordinals + idx * 2) + (USHORT)ordinalBase; |
| 144 | uint32_t name; |
| 145 | auto offset = *(ULONG*)(names + idx * 4); |
| 146 | functionNamesMap[ordinal] = (PCSTR)GetAddress(offset); |
| 147 | } |
| 148 | |
| 149 | for (DWORD i = 0; i < data->NumberOfFunctions; i++) { |
| 150 | ExportedSymbol symbol; |
| 151 | int ordinal = i + (USHORT)ordinalBase; |
| 152 | symbol.Ordinal = ordinal; |
| 153 | bool hasName = false; |
| 154 | auto pos = functionNamesMap.find(ordinal); |
| 155 | if (pos != functionNamesMap.end()) { |
| 156 | hasName = true; |
| 157 | } |
| 158 | if (names && hasName) { |
| 159 | symbol.Name = pos->second; |
| 160 | if (::UnDecorateSymbolName(symbol.Name.c_str(), undecorated, sizeof(undecorated), 0)) |
| 161 | symbol.UndecoratedName = undecorated; |
| 162 | } |
| 163 | DWORD address = *(functions + symbol.Ordinal - ordinalBase); |
| 164 | symbol.Address = address; |
| 165 | //auto offset = RvaToFileOffset(address); |
| 166 | if (hasName) { |
| 167 | if (address > dir->VirtualAddress && address < dir->VirtualAddress + dir->Size) { |
| 168 | symbol.ForwardName = (PCSTR)GetAddress(address); |
| 169 | } |
| 170 | } |
| 171 | exports.push_back(std::move(symbol)); |
| 172 | } |
| 173 | |
| 174 | return exports; |
| 175 | } |
| 176 | |
| 177 | std::vector<ImportedLibrary> PEParser::GetImports() const { |
| 178 | std::vector<ImportedLibrary> libs; |
no test coverage detected