types_win_pe.h 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. // Copyright (c) 2011 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. #ifndef COURGETTE_TYPES_WIN_PE_H_
  5. #define COURGETTE_TYPES_WIN_PE_H_
  6. #include <stddef.h>
  7. #include <stdint.h>
  8. namespace courgette {
  9. // PE file section header. This struct has the same layout as the
  10. // IMAGE_SECTION_HEADER structure from WINNT.H
  11. // http://msdn.microsoft.com/en-us/library/ms680341(VS.85).aspx
  12. //
  13. #pragma pack(push, 1) // Supported by MSVC and GCC. Ensures no gaps in packing.
  14. struct Section {
  15. char name[8];
  16. uint32_t virtual_size;
  17. uint32_t virtual_address;
  18. uint32_t size_of_raw_data;
  19. uint32_t file_offset_of_raw_data;
  20. uint32_t pointer_to_relocations; // Always zero in an image.
  21. uint32_t pointer_to_line_numbers; // Always zero in an image.
  22. uint16_t number_of_relocations; // Always zero in an image.
  23. uint16_t number_of_line_numbers; // Always zero in an image.
  24. uint32_t characteristics;
  25. };
  26. #pragma pack(pop)
  27. static_assert(sizeof(Section) == 40, "section size is 40 bytes");
  28. // ImageDataDirectory has same layout as IMAGE_DATA_DIRECTORY structure from
  29. // WINNT.H
  30. // http://msdn.microsoft.com/en-us/library/ms680305(VS.85).aspx
  31. //
  32. class ImageDataDirectory {
  33. public:
  34. ImageDataDirectory() : address_(0), size_(0) {}
  35. RVA address_;
  36. uint32_t size_;
  37. };
  38. static_assert(sizeof(ImageDataDirectory) == 8,
  39. "image data directory size is 8 bytes");
  40. ////////////////////////////////////////////////////////////////////////////////
  41. // Constants and offsets gleaned from WINNT.H and various articles on the
  42. // format of Windows PE executables.
  43. // This is FIELD_OFFSET(IMAGE_DOS_HEADER, e_lfanew):
  44. const size_t kOffsetOfFileAddressOfNewExeHeader = 0x3c;
  45. const uint16_t kImageNtOptionalHdr32Magic = 0x10b;
  46. const uint16_t kImageNtOptionalHdr64Magic = 0x20b;
  47. const size_t kSizeOfCoffHeader = 20;
  48. const size_t kMinPeHeaderSize = 4 /*signature*/ + kSizeOfCoffHeader;
  49. const size_t kOffsetOfDataDirectoryFromImageOptionalHeader32 = 96;
  50. const size_t kOffsetOfDataDirectoryFromImageOptionalHeader64 = 112;
  51. } // namespace courgette
  52. #endif // COURGETTE_TYPES_WIN_PE_H_