reloc.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. package pe
  2. import (
  3. "encoding/binary"
  4. "errors"
  5. "fmt"
  6. "io"
  7. )
  8. // BaseRelocation is a parsed PE base relocation.
  9. type BaseRelocation struct {
  10. // Relative virtual address of offset (that is, 0 == first byte of PE.)
  11. Offset uint64
  12. // Type of base relocation using the ImageRelBased* constants.
  13. Type int
  14. }
  15. // LoadBaseRelocs loads relocations from memory.
  16. func LoadBaseRelocs(m *Module, mem io.ReadSeeker) []BaseRelocation {
  17. relocs := []BaseRelocation{}
  18. dir := m.Header.OptionalHeader.DataDirectory[ImageDirectoryEntryBaseReloc]
  19. if dir.Size == 0 {
  20. return relocs
  21. }
  22. n := uint32(0)
  23. mem.Seek(int64(dir.VirtualAddress), io.SeekStart)
  24. for n < dir.Size {
  25. hdr := ImageBaseRelocation{}
  26. binary.Read(mem, binary.LittleEndian, &hdr)
  27. data := make([]uint16, hdr.SizeOfBlock/2-4)
  28. binary.Read(mem, binary.LittleEndian, &data)
  29. for _, i := range data {
  30. relocs = append(relocs, BaseRelocation{
  31. Offset: uint64(hdr.VirtualAddress) + uint64(i&0xFFF),
  32. Type: int(i >> 12),
  33. })
  34. }
  35. n += hdr.SizeOfBlock
  36. }
  37. return relocs
  38. }
  39. // Relocate performs a series of relocations on m, where address is the load
  40. // address and original is the original address. The ReadWriteSeeker is
  41. // assumed to have the PE image at offset 0.
  42. func Relocate(machine int, rels []BaseRelocation, address uint64, original uint64, m io.ReadWriteSeeker, o binary.ByteOrder) error {
  43. b := [8]byte{}
  44. delta := address - original
  45. if delta == 0 {
  46. return nil
  47. }
  48. read := func(off uint64, cb int) error {
  49. m.Seek(int64(off), io.SeekStart)
  50. err := readfully(m, b[0:cb])
  51. if err != nil {
  52. return err
  53. }
  54. m.Seek(int64(-cb), io.SeekCurrent)
  55. return nil
  56. }
  57. for _, rel := range rels {
  58. switch rel.Type {
  59. case ImageRelBasedAbsolute:
  60. break
  61. case ImageRelBasedHigh:
  62. if err := read(rel.Offset, 2); err != nil {
  63. return err
  64. }
  65. o.PutUint16(b[0:2], uint16(uint64(o.Uint16(b[:2]))+(delta>>16)))
  66. if _, err := m.Write(b[0:2]); err != nil {
  67. return err
  68. }
  69. break
  70. case ImageRelBasedLow:
  71. if err := read(rel.Offset, 2); err != nil {
  72. return err
  73. }
  74. o.PutUint16(b[0:2], uint16(uint64(o.Uint16(b[:2]))+(delta>>0)))
  75. if _, err := m.Write(b[0:2]); err != nil {
  76. return err
  77. }
  78. break
  79. case ImageRelBasedHighLow:
  80. if err := read(rel.Offset, 4); err != nil {
  81. return err
  82. }
  83. o.PutUint32(b[0:4], uint32(uint64(o.Uint32(b[:4]))+delta))
  84. if _, err := m.Write(b[0:4]); err != nil {
  85. return err
  86. }
  87. break
  88. case ImageRelBasedDir64:
  89. if err := read(rel.Offset, 8); err != nil {
  90. return err
  91. }
  92. o.PutUint64(b[0:8], uint64(o.Uint64(b[:8]))+delta)
  93. if _, err := m.Write(b[0:8]); err != nil {
  94. return err
  95. }
  96. break
  97. // Could use some help for ensuring we have proper support for
  98. // machine-specific relocations. If you are interested in this
  99. // use case for some reason, feel free to send PRs. -- john
  100. case ImageRelBasedMachineSpecific5:
  101. switch machine {
  102. // MIPS: JMP reloc
  103. case ImageFileMachineR3000, ImageFileMachineR3000BE,
  104. ImageFileMachineR4000, ImageFileMachineR10000,
  105. ImageFileMachineWCEMIPSv2, ImageFileMachineMIPS16,
  106. ImageFileMachineMIPSFPU, ImageFileMachineMIPSFPU16:
  107. return errors.New("MIPS JMP reloc not implemented")
  108. // ARM: MOV32 reloc
  109. case ImageFileMachineARM, ImageFileMachineTHUMB,
  110. ImageFileMachineARMNT:
  111. return errors.New("ARM MOV32 reloc not implemented")
  112. // RISC-V: HI20 reloc
  113. case ImageFileMachineRISCV32, ImageFileMachineRISCV64,
  114. ImageFileMachineRISCV128:
  115. return errors.New("RISC-V HI20 reloc not implemented")
  116. default:
  117. return fmt.Errorf("unknown machine-specific relocation type %d on machine type %04x", rel.Type, machine)
  118. }
  119. case ImageRelBasedMachineSpecific7:
  120. switch machine {
  121. // THUMB: MOV32 reloc
  122. case ImageFileMachineARM, ImageFileMachineTHUMB,
  123. ImageFileMachineARMNT:
  124. return errors.New("THUMB MOV32 reloc not implemented")
  125. // RISC-V: LOW12I reloc
  126. case ImageFileMachineRISCV32, ImageFileMachineRISCV64,
  127. ImageFileMachineRISCV128:
  128. return errors.New("RISC-V LOW12I reloc not implemented")
  129. default:
  130. return fmt.Errorf("unknown machine-specific relocation type %d on machine type %04x", rel.Type, machine)
  131. }
  132. case ImageRelBasedMachineSpecific8:
  133. switch machine {
  134. // RISC-V: LOW12S reloc
  135. case ImageFileMachineRISCV32, ImageFileMachineRISCV64,
  136. ImageFileMachineRISCV128:
  137. return errors.New("RISC-V LOW12S reloc not implemented")
  138. default:
  139. return fmt.Errorf("unknown machine-specific relocation type %d on machine type %04x", rel.Type, machine)
  140. }
  141. case ImageRelBasedMachineSpecific9:
  142. switch machine {
  143. // MIPS: JMP16 reloc
  144. case ImageFileMachineR3000, ImageFileMachineR3000BE,
  145. ImageFileMachineR4000, ImageFileMachineR10000,
  146. ImageFileMachineWCEMIPSv2, ImageFileMachineMIPS16,
  147. ImageFileMachineMIPSFPU, ImageFileMachineMIPSFPU16:
  148. return errors.New("MIPS JMP16 reloc not implemented")
  149. // Itanium: Imm64 reloc
  150. case ImageFileMachineIA64:
  151. return errors.New("Itanium Imm64 reloc not implemented")
  152. default:
  153. return fmt.Errorf("unknown machine-specific relocation type %d on machine type %04x", rel.Type, machine)
  154. }
  155. default:
  156. return fmt.Errorf("unknown relocation type %d", rel.Type)
  157. }
  158. }
  159. return nil
  160. }