aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/src/module.cpp
blob: f4f3eb937a88982ea3186dff37aa89d2668eb873 (plain) (blame)
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
// Copyright (c) 2017 Egor Tensin <Egor.Tensin@gmail.com>
// This file is part of the "PDB repository" project.
// For details, see https://github.com/egor-tensin/pdb-repo.
// Distributed under the MIT License.

#include "pdb/all.hpp"

#include <boost/nowide/convert.hpp>

#include <SafeInt.hpp>

#include <cstring>
#include <sstream>
#include <stdexcept>
#include <string>

namespace pdb {

ModuleInfo::ModuleInfo() : ModuleInfo{create_impl()} {}

ModuleInfo::ModuleInfo(const Impl& impl) : impl{impl} {
    if (impl.SizeOfStruct != sizeof(impl))
        throw std::runtime_error{"invalid IMAGEHLP_MODULE64.SizeOfStruct"};
}

ModuleInfo::Impl ModuleInfo::create_impl() {
    Impl impl;
    std::memset(&impl, 0, sizeof(impl));
    impl.SizeOfStruct = sizeof(impl);
    return impl;
}

std::string ModuleInfo::get_name() const {
    return boost::nowide::narrow(impl.ModuleName);
}

Address Module::translate_offline_address(Address offline) const {
    if (offline < get_offline_base())
        throw std::range_error{invalid_offline_address(offline)};
    const auto offset = offline - get_offline_base();
    auto online = offset;
    if (!SafeAdd(online, get_online_base(), online))
        throw std::range_error{invalid_offline_address(offline)};
    return online;
}

Address Module::translate_online_address(Address online) const {
    if (online < get_online_base())
        throw std::range_error{invalid_online_address(online)};
    const auto offset = online - get_online_base();
    auto offline = offset;
    if (!SafeAdd(offline, get_offline_base(), offline))
        throw std::range_error{invalid_online_address(offline)};
    return offline;
}

std::string Module::invalid_offline_address(Address offline) const {
    std::ostringstream oss;
    oss << "offline address " << format_address(offline) << " doesn't belong to module "
        << get_name() << " (base offline address " << format_address(get_offline_base()) << ')';
    return oss.str();
}

std::string Module::invalid_online_address(Address online) const {
    std::ostringstream oss;
    oss << "online address " << format_address(online) << " doesn't belong to module " << get_name()
        << " (base online address " << format_address(get_online_base()) << ')';
    return oss.str();
}

} // namespace pdb