• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 // This file/namespace contains utility functions for gathering
6 // information about PE (Portable Executable) headers within
7 // images (dll's / exe's )
8 
9 #ifndef BASE_IMAGE_UTIL_H_
10 #define BASE_IMAGE_UTIL_H_
11 
12 #include <windows.h>
13 #include <vector>
14 
15 #include "base/basictypes.h"
16 
17 namespace image_util {
18 
19 // Contains both the PE section name (.text, .reloc etc) and its size.
20 struct ImageSectionData {
ImageSectionDataImageSectionData21   ImageSectionData(const std::string& section_name, size_t section_size)
22       : name(section_name),
23         size_in_bytes(section_size) {
24   }
25 
26   std::string name;
27   size_t size_in_bytes;
28 };
29 
30 typedef std::vector<ImageSectionData> ImageSectionsData;
31 
32 // Provides image statistics for modules of a specified process, or for the
33 // specified process' own executable file. To use, invoke CreateImageMetrics()
34 // to get an instance for a specified process, then access the information via
35 // methods.
36 class ImageMetrics {
37  public:
38   // Creates an ImageMetrics instance for given process owned by
39   // the caller.
40   explicit ImageMetrics(HANDLE process);
41   ~ImageMetrics();
42 
43   // Fills a vector of ImageSectionsData containing name/size info
44   // for every section found in the specified dll's PE section table.
45   // The DLL must be loaded by the process associated with this ImageMetrics
46   // instance.
47   bool GetDllImageSectionData(const std::string& loaded_dll_name,
48                               ImageSectionsData* section_sizes);
49 
50   // Fills a vector if ImageSectionsData containing name/size info
51   // for every section found in the executable file of the process
52   // associated with this ImageMetrics instance.
53   bool GetProcessImageSectionData(ImageSectionsData* section_sizes);
54 
55  private:
56   // Helper for GetDllImageSectionData and GetProcessImageSectionData
57   bool GetImageSectionSizes(char* qualified_path, ImageSectionsData* result);
58 
59   HANDLE process_;
60 
61   DISALLOW_COPY_AND_ASSIGN(ImageMetrics);
62 };
63 
64 }  // namespace image_util
65 
66 #endif  // BASE_IMAGE_UTIL_H_
67