layout.rs 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. //! Raw byte offsets of PE32+ header fields, for code that patches images.
  2. //!
  3. //! All offsets are relative to the start of the structure named in the
  4. //! constant's prefix. They follow the published PE/COFF specification.
  5. /// Offset of `e_lfanew` (u32) in the DOS header.
  6. pub const DOS_E_LFANEW: usize = 0x3C;
  7. /// Size of the `PE\0\0` signature that precedes the file header.
  8. pub const NT_SIGNATURE_SIZE: usize = 4;
  9. /// Size of `IMAGE_FILE_HEADER`.
  10. pub const FILE_HEADER_SIZE: usize = 20;
  11. /// `IMAGE_FILE_HEADER::Characteristics` (u16).
  12. pub const FILE_HEADER_CHARACTERISTICS: usize = 18;
  13. /// `IMAGE_OPTIONAL_HEADER64::AddressOfEntryPoint` (u32).
  14. pub const OPT_ADDRESS_OF_ENTRY_POINT: usize = 16;
  15. /// `IMAGE_OPTIONAL_HEADER64::CheckSum` (u32).
  16. pub const OPT_CHECKSUM: usize = 64;
  17. /// `IMAGE_OPTIONAL_HEADER64::DllCharacteristics` (u16).
  18. pub const OPT_DLL_CHARACTERISTICS: usize = 70;
  19. /// `IMAGE_OPTIONAL_HEADER64::NumberOfRvaAndSizes` (u32).
  20. pub const OPT_NUMBER_OF_RVA_AND_SIZES: usize = 108;
  21. /// First `IMAGE_DATA_DIRECTORY` entry (each is `{ rva: u32, size: u32 }`).
  22. pub const OPT_DATA_DIRECTORY: usize = 112;
  23. /// Size of one `IMAGE_DATA_DIRECTORY`.
  24. pub const DATA_DIRECTORY_SIZE: usize = 8;
  25. /// `IMAGE_TLS_DIRECTORY64::AddressOfCallBacks` (u64 VA).
  26. pub const TLS64_ADDRESS_OF_CALLBACKS: usize = 24;
  27. /// Data directory indices.
  28. pub mod dir {
  29. pub const IMPORT: usize = 1;
  30. pub const EXCEPTION: usize = 3;
  31. pub const BASERELOC: usize = 5;
  32. pub const LOAD_CONFIG: usize = 10;
  33. pub const BOUND_IMPORT: usize = 11;
  34. }
  35. /// `IMAGE_FILE_HEADER::Characteristics` flags.
  36. pub mod file_flags {
  37. pub const RELOCS_STRIPPED: u16 = 0x0001;
  38. pub const DLL: u16 = 0x2000;
  39. }
  40. /// `IMAGE_OPTIONAL_HEADER64::DllCharacteristics` flags.
  41. pub mod dll_flags {
  42. pub const HIGH_ENTROPY_VA: u16 = 0x0020;
  43. pub const DYNAMIC_BASE: u16 = 0x0040;
  44. pub const FORCE_INTEGRITY: u16 = 0x0080;
  45. pub const NX_COMPAT: u16 = 0x0100;
  46. pub const APPCONTAINER: u16 = 0x1000;
  47. pub const GUARD_CF: u16 = 0x4000;
  48. }
  49. /// `IMAGE_OPTIONAL_HEADER64::Subsystem` values.
  50. pub mod subsystem {
  51. pub const WINDOWS_GUI: u16 = 2;
  52. pub const WINDOWS_CUI: u16 = 3;
  53. pub const XBOX: u16 = 14;
  54. pub const XBOX_CODE_CATALOG: u16 = 17;
  55. }
  56. /// Offset of the optional header from the start of the file.
  57. pub fn optional_header_offset(data: &[u8]) -> Option<usize> {
  58. let lfanew = read_u32(data, DOS_E_LFANEW)? as usize;
  59. Some(lfanew + NT_SIGNATURE_SIZE + FILE_HEADER_SIZE)
  60. }
  61. /// Offset of the file header from the start of the file.
  62. pub fn file_header_offset(data: &[u8]) -> Option<usize> {
  63. let lfanew = read_u32(data, DOS_E_LFANEW)? as usize;
  64. Some(lfanew + NT_SIGNATURE_SIZE)
  65. }
  66. /// Offset of data directory `index` from the start of the file.
  67. pub fn data_directory_offset(data: &[u8], index: usize) -> Option<usize> {
  68. let opt = optional_header_offset(data)?;
  69. let count = read_u32(data, opt + OPT_NUMBER_OF_RVA_AND_SIZES)? as usize;
  70. (index < count).then_some(opt + OPT_DATA_DIRECTORY + index * DATA_DIRECTORY_SIZE)
  71. }
  72. pub fn read_u32(data: &[u8], at: usize) -> Option<u32> {
  73. Some(u32::from_le_bytes(data.get(at..at + 4)?.try_into().ok()?))
  74. }
  75. pub fn read_u64(data: &[u8], at: usize) -> Option<u64> {
  76. Some(u64::from_le_bytes(data.get(at..at + 8)?.try_into().ok()?))
  77. }