cpuelf.pas 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. {
  2. Copyright (c) 1998-2006 by Peter Vreman
  3. Includes ELF-related code specific to x86_64
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit cpuelf;
  18. {$i fpcdefs.inc}
  19. interface
  20. implementation
  21. uses
  22. verbose,
  23. systems,ogbase,ogelf,assemble;
  24. type
  25. TElfObjOutputx86_64=class(TElfObjectOutput)
  26. function encodereloc(objrel:TObjRelocation):byte;override;
  27. end;
  28. TElfAssemblerx86_64=class(TInternalAssembler)
  29. constructor create(smart:boolean);override;
  30. end;
  31. const
  32. { Relocation types }
  33. R_X86_64_NONE = 0;
  34. R_X86_64_64 = 1; { Direct 64 bit }
  35. R_X86_64_PC32 = 2; { PC relative 32 bit signed }
  36. R_X86_64_GOT32 = 3; { 32 bit GOT entry }
  37. R_X86_64_PLT32 = 4; { 32 bit PLT address }
  38. R_X86_64_COPY = 5; { Copy symbol at runtime }
  39. R_X86_64_GLOB_DAT = 6; { Create GOT entry }
  40. R_X86_64_JUMP_SLOT = 7; { Create PLT entry }
  41. R_X86_64_RELATIVE = 8; { Adjust by program base }
  42. R_X86_64_GOTPCREL = 9; { 32 bit signed PC relative offset to GOT }
  43. R_X86_64_32 = 10; { Direct 32 bit zero extended }
  44. R_X86_64_32S = 11; { Direct 32 bit sign extended }
  45. R_X86_64_16 = 12; { Direct 16 bit zero extended }
  46. R_X86_64_PC16 = 13; { 16 bit sign extended PC relative }
  47. R_X86_64_8 = 14; { Direct 8 bit sign extended }
  48. R_X86_64_PC8 = 15; { 8 bit sign extended PC relative }
  49. R_X86_64_DTPMOD64 = 16; { ID of module containing symbol }
  50. R_X86_64_DTPOFF64 = 17; { Offset in module's TLS block }
  51. R_X86_64_TPOFF64 = 18; { Offset in initial TLS block }
  52. { 32 bit signed PC relative offset to two GOT entries for GD symbol }
  53. R_X86_64_TLSGD = 19;
  54. { 32 bit signed PC relative offset to two GOT entries for LD symbol }
  55. R_X86_64_TLSLD = 20;
  56. R_X86_64_DTPOFF32 = 21; { Offset in TLS block }
  57. { 32 bit signed PC relative offset to GOT entry for IE symbol }
  58. R_X86_64_GOTTPOFF = 22;
  59. R_X86_64_TPOFF32 = 23; { Offset in initial TLS block }
  60. R_X86_64_PC64 = 24; { PC relative 64-bit signed }
  61. R_X86_64_GOTOFF64 = 25; { 64-bit offset from GOT base }
  62. R_X86_64_GOTPC32 = 26; { PC-relative offset GOT }
  63. R_X86_64_GOT64 = 27; { 64-bit GOT entry offset }
  64. R_X86_64_GOTPCREL64 = 28; { 64-bit PC relative offset to GOT entry }
  65. R_X86_64_GOTPC64 = 29; { 64-bit PC relative offset to GOT }
  66. R_X86_64_GOTPLT64 = 30; { Like GOT64, indicates that PLT entry needed }
  67. R_X86_64_PLTOFF64 = 31; { 64-bit GOT relative offset to PLT entry }
  68. R_X86_64_SIZE32 = 32;
  69. R_X86_64_SIZE64 = 33;
  70. R_X86_64_GOTPC32_TLSDESC = 34;
  71. R_X86_64_TLSDESC_CALL = 35;
  72. R_X86_64_TLSDESC = 36;
  73. R_X86_64_IRELATIVE = 37;
  74. R_X86_64_GNU_VTINHERIT = 250; { GNU extension to record C++ vtable hierarchy }
  75. R_X86_64_GNU_VTENTRY = 251; { GNU extension to record C++ vtable member usage }
  76. {****************************************************************************
  77. TELFObjectOutputx86_64
  78. ****************************************************************************}
  79. function TElfObjOutputx86_64.encodereloc(objrel:TObjRelocation):byte;
  80. begin
  81. case objrel.typ of
  82. { Note: 8 and 16-bit relocations are known to be non-conformant with
  83. AMD64 ABI, so they aren't handled. }
  84. RELOC_RELATIVE :
  85. if objrel.size=8 then
  86. result:=R_X86_64_PC64
  87. else if objrel.size=4 then
  88. result:=R_X86_64_PC32
  89. else
  90. InternalError(2012061900);
  91. RELOC_ABSOLUTE :
  92. if objrel.size=8 then
  93. result:=R_X86_64_64
  94. else if objrel.size=4 then
  95. result:=R_X86_64_32
  96. else
  97. InternalError(2012061901);
  98. RELOC_ABSOLUTE32 :
  99. result:=R_X86_64_32S;
  100. RELOC_GOTPCREL :
  101. result:=R_X86_64_GOTPCREL;
  102. RELOC_PLT32 :
  103. result:=R_X86_64_PLT32;
  104. else
  105. result:=0;
  106. InternalError(2012082302);
  107. end;
  108. end;
  109. {****************************************************************************
  110. TELFAssemblerx86_64
  111. ****************************************************************************}
  112. constructor TElfAssemblerx86_64.create(smart:boolean);
  113. begin
  114. inherited Create(smart);
  115. CObjOutput:=TElfObjOutputx86_64;
  116. end;
  117. {*****************************************************************************
  118. Initialize
  119. *****************************************************************************}
  120. const
  121. as_x86_64_elf64_info : tasminfo =
  122. (
  123. id : as_x86_64_elf64;
  124. idtxt : 'ELF';
  125. asmbin : '';
  126. asmcmd : '';
  127. supported_targets : [system_x86_64_linux,system_x86_64_freebsd,
  128. system_x86_64_openbsd,system_x86_64_netbsd];
  129. flags : [af_outputbinary,af_smartlink_sections,af_supports_dwarf];
  130. labelprefix : '.L';
  131. comment : '';
  132. dollarsign: '$';
  133. );
  134. initialization
  135. RegisterAssembler(as_x86_64_elf64_info,TElfAssemblerx86_64);
  136. end.