少年修仙传客户端基础资源
hch
2024-04-01 d01413b00ef631ac20347716b23818b0b811f65f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#include "MSVCMapFileParser.h"
#include "MSVCSectionParser.h"
#include "MSVCSymbolParser.h"
#include <sstream>
#include <algorithm>
#include <stdexcept>
#include <string>
#include <cstring>
#include <cstdio>
 
 
namespace mapfileparser
{
    static size_t GetSectionIndexForSymbol(int64_t absoluteSymbolAddress, const std::vector<Section>& sections)
    {
        for (size_t index = 0; index < sections.size(); ++index)
        {
            if (absoluteSymbolAddress >= sections[index].start && absoluteSymbolAddress <= (sections[index].start + sections[index].length))
                return index;
        }
 
        throw std::runtime_error(std::string("Unable to find section for symbol"));
    }
 
    static void CalculateLengthsAndSegmentTypesForAllSymbols(MapFile& mapFile)
    {
        bool processedImageBase = false;
        // iterate all symbols and calculate length based on difference between start and start of next symbol
        for (size_t index = 0; index < mapFile.symbols.size(); ++index)
        {
            if (!processedImageBase)
            {
                if (mapFile.symbols[index].name == "___ImageBase" || mapFile.symbols[index].name == "__ImageBase")
                    processedImageBase = true;
 
                continue;
            }
 
            size_t currentSectionIndex = GetSectionIndexForSymbol(mapFile.symbols[index].start, mapFile.sections);
 
            mapFile.symbols[index].segmentType = mapFile.sections[currentSectionIndex].segmentType;
 
            if (index == mapFile.symbols.size() - 1 || mapFile.sections[currentSectionIndex].segmentName != mapFile.sections[GetSectionIndexForSymbol(mapFile.symbols[index + 1].start, mapFile.sections)].segmentName)
                mapFile.symbols[index].length = mapFile.sections[currentSectionIndex].start + mapFile.sections[currentSectionIndex].length - mapFile.symbols[index].start;
            else
                mapFile.symbols[index].length = mapFile.symbols[index + 1].start - mapFile.symbols[index].start;
        }
    }
 
    static std::string ReadUntilDelimiter(std::istream& is, const char* delimiter)
    {
        const size_t delimiterLength = strlen(delimiter);
        std::string line;
        std::getline(is, line);
        while (!is.eof() && strncmp(line.c_str(), delimiter, delimiterLength) != 0)
            std::getline(is, line);
 
        return line;
    }
 
    static int64_t ParseBaseAddress(std::istream& is)
    {
        const char* preferredLoadAddressDelimiter = " Preferred load address is ";
        const size_t preferredLoadAddressDelimiterLength = strlen(preferredLoadAddressDelimiter);
 
        std::string line = ReadUntilDelimiter(is, preferredLoadAddressDelimiter);
 
        std::string imageBaseString = line.substr(preferredLoadAddressDelimiterLength);
        int64_t imageBaseOffset = 0;
        int fieldsParsed = sscanf(imageBaseString.c_str(), "%llX", &imageBaseOffset);
        if (fieldsParsed != 1)
            throw std::runtime_error("Unable to parse base address.");
 
        return imageBaseOffset;
    }
 
    static void ParseSections(std::istream& is, std::vector<Section>& sections)
    {
        std::string line;
        std::getline(is, line);
        int64_t segmentOffset = 0x1000;
        std::string currentSegmentName;
        while (!is.eof() && line.length() > 0)
        {
            Section section = MSVCSectionParser::Parse(line);
            if (currentSegmentName.length() == 0)
                currentSegmentName = section.segmentName;
            else if (currentSegmentName != section.segmentName)
            {
                Section prevSection = sections[sections.size() - 1];
                const int32_t kSectionBoundarySize = 0x1000;
                segmentOffset = prevSection.start + prevSection.length;
                if ((segmentOffset % kSectionBoundarySize) != 0)
                    segmentOffset = (segmentOffset + kSectionBoundarySize) / kSectionBoundarySize * kSectionBoundarySize;
                currentSegmentName = section.segmentName;
            }
 
            section.start += segmentOffset;
            sections.push_back(section);
 
            std::getline(is, line);
        }
    }
 
    static void ParseSymbols(std::istream& is, std::vector<Symbol>& symbols, int64_t imageBaseOffset)
    {
        std::string line;
        std::getline(is, line);
 
        while (!is.eof())
        {
            std::getline(is, line);
            if (line.length() == 0)
                break;
 
            symbols.push_back(MSVCSymbolParser::Parse(line, imageBaseOffset));
        }
    }
 
    MapFile MSVCMapFileParser::Parse(std::istream& is)
    {
        MapFile mapFile;
 
        int64_t imageBaseOffset = ParseBaseAddress(is);
 
        ReadUntilDelimiter(is, " Start ");
 
        ParseSections(is, mapFile.sections);
 
        ReadUntilDelimiter(is, "  Address ");
 
        ParseSymbols(is, mapFile.symbols, imageBaseOffset);
 
        CalculateLengthsAndSegmentTypesForAllSymbols(mapFile);
 
        return mapFile;
    }
}