coff-x86_64.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010
  1. /* BFD back-end for AMD 64 COFF files.
  2. Copyright (C) 2006-2022 Free Software Foundation, Inc.
  3. This file is part of BFD, the Binary File Descriptor library.
  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 3 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., 51 Franklin Street - Fifth Floor, Boston,
  15. MA 02110-1301, USA.
  16. Written by Kai Tietz, OneVision Software GmbH&CoKg. */
  17. /* Note we have to make sure not to include headers twice.
  18. Not all headers are wrapped in #ifdef guards, so we define
  19. PEI_HEADERS to prevent double including here. */
  20. #ifndef PEI_HEADERS
  21. #include "sysdep.h"
  22. #include "bfd.h"
  23. #include "libbfd.h"
  24. #include "coff/x86_64.h"
  25. #include "coff/internal.h"
  26. #include "libcoff.h"
  27. #include "libiberty.h"
  28. #endif
  29. #define BADMAG(x) AMD64BADMAG(x)
  30. #ifdef COFF_WITH_pex64
  31. # undef AOUTSZ
  32. # define AOUTSZ PEPAOUTSZ
  33. # define PEAOUTHDR PEPAOUTHDR
  34. #endif
  35. #define COFF_DEFAULT_SECTION_ALIGNMENT_POWER (2)
  36. /* The page size is a guess based on ELF. */
  37. #define COFF_PAGE_SIZE 0x1000
  38. /* All users of this file have bfd_octets_per_byte (abfd, sec) == 1. */
  39. #define OCTETS_PER_BYTE(ABFD, SEC) 1
  40. /* For some reason when using AMD COFF the value stored in the .text
  41. section for a reference to a common symbol is the value itself plus
  42. any desired offset. Ian Taylor, Cygnus Support. */
  43. /* If we are producing relocatable output, we need to do some
  44. adjustments to the object file that are not done by the
  45. bfd_perform_relocation function. This function is called by every
  46. reloc type to make any required adjustments. */
  47. static bfd_reloc_status_type
  48. coff_amd64_reloc (bfd *abfd,
  49. arelent *reloc_entry,
  50. asymbol *symbol,
  51. void * data,
  52. asection *input_section ATTRIBUTE_UNUSED,
  53. bfd *output_bfd,
  54. char **error_message ATTRIBUTE_UNUSED)
  55. {
  56. symvalue diff;
  57. #if !defined (COFF_WITH_PE)
  58. if (output_bfd == NULL)
  59. return bfd_reloc_continue;
  60. #endif
  61. if (bfd_is_com_section (symbol->section))
  62. {
  63. #if !defined (COFF_WITH_PE)
  64. /* We are relocating a common symbol. The current value in the
  65. object file is ORIG + OFFSET, where ORIG is the value of the
  66. common symbol as seen by the object file when it was compiled
  67. (this may be zero if the symbol was undefined) and OFFSET is
  68. the offset into the common symbol (normally zero, but may be
  69. non-zero when referring to a field in a common structure).
  70. ORIG is the negative of reloc_entry->addend, which is set by
  71. the CALC_ADDEND macro below. We want to replace the value in
  72. the object file with NEW + OFFSET, where NEW is the value of
  73. the common symbol which we are going to put in the final
  74. object file. NEW is symbol->value. */
  75. diff = symbol->value + reloc_entry->addend;
  76. #else
  77. /* In PE mode, we do not offset the common symbol. */
  78. diff = reloc_entry->addend;
  79. #endif
  80. }
  81. else
  82. {
  83. /* For some reason bfd_perform_relocation always effectively
  84. ignores the addend for a COFF target when producing
  85. relocatable output. This seems to be always wrong for 386
  86. COFF, so we handle the addend here instead. */
  87. #if defined (COFF_WITH_PE)
  88. if (output_bfd == NULL)
  89. {
  90. if (symbol->flags & BSF_WEAK)
  91. diff = reloc_entry->addend - symbol->value;
  92. else
  93. diff = -reloc_entry->addend;
  94. }
  95. else
  96. #endif
  97. diff = reloc_entry->addend;
  98. }
  99. #if defined (COFF_WITH_PE)
  100. if (output_bfd == NULL)
  101. {
  102. /* PC relative relocations are off by their size. */
  103. if (reloc_entry->howto->pc_relative)
  104. diff -= bfd_get_reloc_size (reloc_entry->howto);
  105. if (reloc_entry->howto->type >= R_AMD64_PCRLONG_1
  106. && reloc_entry->howto->type <= R_AMD64_PCRLONG_5)
  107. diff -= reloc_entry->howto->type - R_AMD64_PCRLONG;
  108. }
  109. if (reloc_entry->howto->type == R_AMD64_IMAGEBASE
  110. && output_bfd == NULL)
  111. {
  112. bfd *obfd = input_section->output_section->owner;
  113. struct bfd_link_info *link_info;
  114. struct bfd_link_hash_entry *h;
  115. switch (bfd_get_flavour (obfd))
  116. {
  117. case bfd_target_coff_flavour:
  118. diff -= pe_data (obfd)->pe_opthdr.ImageBase;
  119. break;
  120. case bfd_target_elf_flavour:
  121. /* Subtract __ImageBase. */
  122. link_info = _bfd_get_link_info (obfd);
  123. if (link_info == NULL)
  124. return bfd_reloc_dangerous;
  125. h = bfd_link_hash_lookup (link_info->hash, "__ImageBase",
  126. false, false, false);
  127. if (h == NULL)
  128. return bfd_reloc_dangerous;
  129. while (h->type == bfd_link_hash_indirect)
  130. h = h->u.i.link;
  131. /* ELF symbols in relocatable files are section relative,
  132. but in nonrelocatable files they are virtual addresses. */
  133. diff -= (h->u.def.value
  134. + h->u.def.section->output_offset
  135. + h->u.def.section->output_section->vma);
  136. break;
  137. default:
  138. break;
  139. }
  140. }
  141. #endif
  142. #define DOIT(x) \
  143. x = ((x & ~howto->dst_mask) | (((x & howto->src_mask) + diff) & howto->dst_mask))
  144. if (diff != 0)
  145. {
  146. reloc_howto_type *howto = reloc_entry->howto;
  147. bfd_size_type octets = (reloc_entry->address
  148. * OCTETS_PER_BYTE (abfd, input_section));
  149. unsigned char *addr = (unsigned char *) data + octets;
  150. if (!bfd_reloc_offset_in_range (howto, abfd, input_section, octets))
  151. return bfd_reloc_outofrange;
  152. switch (howto->size)
  153. {
  154. case 0:
  155. {
  156. char x = bfd_get_8 (abfd, addr);
  157. DOIT (x);
  158. bfd_put_8 (abfd, x, addr);
  159. }
  160. break;
  161. case 1:
  162. {
  163. short x = bfd_get_16 (abfd, addr);
  164. DOIT (x);
  165. bfd_put_16 (abfd, (bfd_vma) x, addr);
  166. }
  167. break;
  168. case 2:
  169. {
  170. long x = bfd_get_32 (abfd, addr);
  171. DOIT (x);
  172. bfd_put_32 (abfd, (bfd_vma) x, addr);
  173. }
  174. break;
  175. case 4:
  176. {
  177. bfd_uint64_t x = bfd_get_64 (abfd, addr);
  178. DOIT (x);
  179. bfd_put_64 (abfd, x, addr);
  180. }
  181. break;
  182. default:
  183. bfd_set_error (bfd_error_bad_value);
  184. return bfd_reloc_notsupported;
  185. }
  186. }
  187. /* Now let bfd_perform_relocation finish everything up. */
  188. return bfd_reloc_continue;
  189. }
  190. #if defined(COFF_WITH_PE)
  191. /* Return TRUE if this relocation should appear in the output .reloc
  192. section. */
  193. static bool
  194. in_reloc_p (bfd *abfd ATTRIBUTE_UNUSED, reloc_howto_type *howto)
  195. {
  196. return ! howto->pc_relative
  197. && howto->type != R_AMD64_IMAGEBASE
  198. && howto->type != R_AMD64_SECREL
  199. && howto->type != R_AMD64_SECTION;
  200. }
  201. #endif /* COFF_WITH_PE */
  202. #ifndef PCRELOFFSET
  203. #define PCRELOFFSET true
  204. #endif
  205. static reloc_howto_type howto_table[] =
  206. {
  207. EMPTY_HOWTO (0),
  208. HOWTO (R_AMD64_DIR64, /* type 1*/
  209. 0, /* rightshift */
  210. 4, /* size (0 = byte, 1 = short, 2 = long, 4 = long long) */
  211. 64, /* bitsize */
  212. false, /* pc_relative */
  213. 0, /* bitpos */
  214. complain_overflow_bitfield, /* complain_on_overflow */
  215. coff_amd64_reloc, /* special_function */
  216. "IMAGE_REL_AMD64_ADDR64", /* name */
  217. true, /* partial_inplace */
  218. 0xffffffffffffffffll, /* src_mask */
  219. 0xffffffffffffffffll, /* dst_mask */
  220. true), /* pcrel_offset */
  221. HOWTO (R_AMD64_DIR32, /* type 2 */
  222. 0, /* rightshift */
  223. 2, /* size (0 = byte, 1 = short, 2 = long) */
  224. 32, /* bitsize */
  225. false, /* pc_relative */
  226. 0, /* bitpos */
  227. complain_overflow_bitfield, /* complain_on_overflow */
  228. coff_amd64_reloc, /* special_function */
  229. "IMAGE_REL_AMD64_ADDR32", /* name */
  230. true, /* partial_inplace */
  231. 0xffffffff, /* src_mask */
  232. 0xffffffff, /* dst_mask */
  233. true), /* pcrel_offset */
  234. /* PE IMAGE_REL_AMD64_ADDR32NB relocation (3). */
  235. HOWTO (R_AMD64_IMAGEBASE, /* type */
  236. 0, /* rightshift */
  237. 2, /* size (0 = byte, 1 = short, 2 = long) */
  238. 32, /* bitsize */
  239. false, /* pc_relative */
  240. 0, /* bitpos */
  241. complain_overflow_bitfield, /* complain_on_overflow */
  242. coff_amd64_reloc, /* special_function */
  243. "IMAGE_REL_AMD64_ADDR32NB", /* name */
  244. true, /* partial_inplace */
  245. 0xffffffff, /* src_mask */
  246. 0xffffffff, /* dst_mask */
  247. false), /* pcrel_offset */
  248. /* 32-bit longword PC relative relocation (4). */
  249. HOWTO (R_AMD64_PCRLONG, /* type 4 */
  250. 0, /* rightshift */
  251. 2, /* size (0 = byte, 1 = short, 2 = long) */
  252. 32, /* bitsize */
  253. true, /* pc_relative */
  254. 0, /* bitpos */
  255. complain_overflow_signed, /* complain_on_overflow */
  256. coff_amd64_reloc, /* special_function */
  257. "IMAGE_REL_AMD64_REL32", /* name */
  258. true, /* partial_inplace */
  259. 0xffffffff, /* src_mask */
  260. 0xffffffff, /* dst_mask */
  261. PCRELOFFSET), /* pcrel_offset */
  262. HOWTO (R_AMD64_PCRLONG_1, /* type 5 */
  263. 0, /* rightshift */
  264. 2, /* size (0 = byte, 1 = short, 2 = long) */
  265. 32, /* bitsize */
  266. true, /* pc_relative */
  267. 0, /* bitpos */
  268. complain_overflow_signed, /* complain_on_overflow */
  269. coff_amd64_reloc, /* special_function */
  270. "IMAGE_REL_AMD64_REL32_1", /* name */
  271. true, /* partial_inplace */
  272. 0xffffffff, /* src_mask */
  273. 0xffffffff, /* dst_mask */
  274. PCRELOFFSET), /* pcrel_offset */
  275. HOWTO (R_AMD64_PCRLONG_2, /* type 6 */
  276. 0, /* rightshift */
  277. 2, /* size (0 = byte, 1 = short, 2 = long) */
  278. 32, /* bitsize */
  279. true, /* pc_relative */
  280. 0, /* bitpos */
  281. complain_overflow_signed, /* complain_on_overflow */
  282. coff_amd64_reloc, /* special_function */
  283. "IMAGE_REL_AMD64_REL32_2", /* name */
  284. true, /* partial_inplace */
  285. 0xffffffff, /* src_mask */
  286. 0xffffffff, /* dst_mask */
  287. PCRELOFFSET), /* pcrel_offset */
  288. HOWTO (R_AMD64_PCRLONG_3, /* type 7 */
  289. 0, /* rightshift */
  290. 2, /* size (0 = byte, 1 = short, 2 = long) */
  291. 32, /* bitsize */
  292. true, /* pc_relative */
  293. 0, /* bitpos */
  294. complain_overflow_signed, /* complain_on_overflow */
  295. coff_amd64_reloc, /* special_function */
  296. "IMAGE_REL_AMD64_REL32_3", /* name */
  297. true, /* partial_inplace */
  298. 0xffffffff, /* src_mask */
  299. 0xffffffff, /* dst_mask */
  300. PCRELOFFSET), /* pcrel_offset */
  301. HOWTO (R_AMD64_PCRLONG_4, /* type 8 */
  302. 0, /* rightshift */
  303. 2, /* size (0 = byte, 1 = short, 2 = long) */
  304. 32, /* bitsize */
  305. true, /* pc_relative */
  306. 0, /* bitpos */
  307. complain_overflow_signed, /* complain_on_overflow */
  308. coff_amd64_reloc, /* special_function */
  309. "IMAGE_REL_AMD64_REL32_4", /* name */
  310. true, /* partial_inplace */
  311. 0xffffffff, /* src_mask */
  312. 0xffffffff, /* dst_mask */
  313. PCRELOFFSET), /* pcrel_offset */
  314. HOWTO (R_AMD64_PCRLONG_5, /* type 9 */
  315. 0, /* rightshift */
  316. 2, /* size (0 = byte, 1 = short, 2 = long) */
  317. 32, /* bitsize */
  318. true, /* pc_relative */
  319. 0, /* bitpos */
  320. complain_overflow_signed, /* complain_on_overflow */
  321. coff_amd64_reloc, /* special_function */
  322. "IMAGE_REL_AMD64_REL32_5", /* name */
  323. true, /* partial_inplace */
  324. 0xffffffff, /* src_mask */
  325. 0xffffffff, /* dst_mask */
  326. PCRELOFFSET), /* pcrel_offset */
  327. #if defined(COFF_WITH_PE)
  328. /* 16-bit word section relocation (10). */
  329. HOWTO (R_AMD64_SECTION, /* type */
  330. 0, /* rightshift */
  331. 1, /* size (0 = byte, 1 = short, 2 = long) */
  332. 16, /* bitsize */
  333. false, /* pc_relative */
  334. 0, /* bitpos */
  335. complain_overflow_bitfield, /* complain_on_overflow */
  336. coff_amd64_reloc, /* special_function */
  337. "IMAGE_REL_AMD64_SECTION", /* name */
  338. true, /* partial_inplace */
  339. 0x0000ffff, /* src_mask */
  340. 0x0000ffff, /* dst_mask */
  341. true),
  342. /* 32-bit longword section relative relocation (11). */
  343. HOWTO (R_AMD64_SECREL, /* type */
  344. 0, /* rightshift */
  345. 2, /* size (0 = byte, 1 = short, 2 = long) */
  346. 32, /* bitsize */
  347. false, /* pc_relative */
  348. 0, /* bitpos */
  349. complain_overflow_bitfield, /* complain_on_overflow */
  350. coff_amd64_reloc, /* special_function */
  351. "IMAGE_REL_AMD64_SECREL", /* name */
  352. true, /* partial_inplace */
  353. 0xffffffff, /* src_mask */
  354. 0xffffffff, /* dst_mask */
  355. true), /* pcrel_offset */
  356. #else
  357. EMPTY_HOWTO (10),
  358. EMPTY_HOWTO (11),
  359. #endif
  360. EMPTY_HOWTO (12),
  361. EMPTY_HOWTO (13),
  362. #ifndef DONT_EXTEND_AMD64
  363. HOWTO (R_AMD64_PCRQUAD,
  364. 0, /* rightshift */
  365. 4, /* size (0 = byte, 1 = short, 2 = long) */
  366. 64, /* bitsize */
  367. true, /* pc_relative */
  368. 0, /* bitpos */
  369. complain_overflow_signed, /* complain_on_overflow */
  370. coff_amd64_reloc, /* special_function */
  371. "R_X86_64_PC64", /* name */
  372. true, /* partial_inplace */
  373. 0xffffffffffffffffll, /* src_mask */
  374. 0xffffffffffffffffll, /* dst_mask */
  375. PCRELOFFSET), /* pcrel_offset */
  376. #else
  377. EMPTY_HOWTO (14),
  378. #endif
  379. /* Byte relocation (15). */
  380. HOWTO (R_RELBYTE, /* type */
  381. 0, /* rightshift */
  382. 0, /* size (0 = byte, 1 = short, 2 = long) */
  383. 8, /* bitsize */
  384. false, /* pc_relative */
  385. 0, /* bitpos */
  386. complain_overflow_bitfield, /* complain_on_overflow */
  387. coff_amd64_reloc, /* special_function */
  388. "R_X86_64_8", /* name */
  389. true, /* partial_inplace */
  390. 0x000000ff, /* src_mask */
  391. 0x000000ff, /* dst_mask */
  392. PCRELOFFSET), /* pcrel_offset */
  393. /* 16-bit word relocation (16). */
  394. HOWTO (R_RELWORD, /* type */
  395. 0, /* rightshift */
  396. 1, /* size (0 = byte, 1 = short, 2 = long) */
  397. 16, /* bitsize */
  398. false, /* pc_relative */
  399. 0, /* bitpos */
  400. complain_overflow_bitfield, /* complain_on_overflow */
  401. coff_amd64_reloc, /* special_function */
  402. "R_X86_64_16", /* name */
  403. true, /* partial_inplace */
  404. 0x0000ffff, /* src_mask */
  405. 0x0000ffff, /* dst_mask */
  406. PCRELOFFSET), /* pcrel_offset */
  407. /* 32-bit longword relocation (17). */
  408. HOWTO (R_RELLONG, /* type */
  409. 0, /* rightshift */
  410. 2, /* size (0 = byte, 1 = short, 2 = long) */
  411. 32, /* bitsize */
  412. false, /* pc_relative */
  413. 0, /* bitpos */
  414. complain_overflow_bitfield, /* complain_on_overflow */
  415. coff_amd64_reloc, /* special_function */
  416. "R_X86_64_32S", /* name */
  417. true, /* partial_inplace */
  418. 0xffffffff, /* src_mask */
  419. 0xffffffff, /* dst_mask */
  420. PCRELOFFSET), /* pcrel_offset */
  421. /* Byte PC relative relocation (18). */
  422. HOWTO (R_PCRBYTE, /* type */
  423. 0, /* rightshift */
  424. 0, /* size (0 = byte, 1 = short, 2 = long) */
  425. 8, /* bitsize */
  426. true, /* pc_relative */
  427. 0, /* bitpos */
  428. complain_overflow_signed, /* complain_on_overflow */
  429. coff_amd64_reloc, /* special_function */
  430. "R_X86_64_PC8", /* name */
  431. true, /* partial_inplace */
  432. 0x000000ff, /* src_mask */
  433. 0x000000ff, /* dst_mask */
  434. PCRELOFFSET), /* pcrel_offset */
  435. /* 16-bit word PC relative relocation (19). */
  436. HOWTO (R_PCRWORD, /* type */
  437. 0, /* rightshift */
  438. 1, /* size (0 = byte, 1 = short, 2 = long) */
  439. 16, /* bitsize */
  440. true, /* pc_relative */
  441. 0, /* bitpos */
  442. complain_overflow_signed, /* complain_on_overflow */
  443. coff_amd64_reloc, /* special_function */
  444. "R_X86_64_PC16", /* name */
  445. true, /* partial_inplace */
  446. 0x0000ffff, /* src_mask */
  447. 0x0000ffff, /* dst_mask */
  448. PCRELOFFSET), /* pcrel_offset */
  449. /* 32-bit longword PC relative relocation (20). */
  450. HOWTO (R_PCRLONG, /* type */
  451. 0, /* rightshift */
  452. 2, /* size (0 = byte, 1 = short, 2 = long) */
  453. 32, /* bitsize */
  454. true, /* pc_relative */
  455. 0, /* bitpos */
  456. complain_overflow_signed, /* complain_on_overflow */
  457. coff_amd64_reloc, /* special_function */
  458. "R_X86_64_PC32", /* name */
  459. true, /* partial_inplace */
  460. 0xffffffff, /* src_mask */
  461. 0xffffffff, /* dst_mask */
  462. PCRELOFFSET) /* pcrel_offset */
  463. };
  464. #define NUM_HOWTOS ARRAY_SIZE (howto_table)
  465. /* Turn a howto into a reloc nunmber */
  466. #define SELECT_RELOC(x,howto) { x.r_type = howto->type; }
  467. #define I386 1 /* Customize coffcode.h */
  468. #define AMD64 1
  469. #define RTYPE2HOWTO(cache_ptr, dst) \
  470. ((cache_ptr)->howto = \
  471. ((dst)->r_type < NUM_HOWTOS) \
  472. ? howto_table + (dst)->r_type \
  473. : NULL)
  474. /* For 386 COFF a STYP_NOLOAD | STYP_BSS section is part of a shared
  475. library. On some other COFF targets STYP_BSS is normally
  476. STYP_NOLOAD. */
  477. #define BSS_NOLOAD_IS_SHARED_LIBRARY
  478. /* Compute the addend of a reloc. If the reloc is to a common symbol,
  479. the object file contains the value of the common symbol. By the
  480. time this is called, the linker may be using a different symbol
  481. from a different object file with a different value. Therefore, we
  482. hack wildly to locate the original symbol from this file so that we
  483. can make the correct adjustment. This macro sets coffsym to the
  484. symbol from the original file, and uses it to set the addend value
  485. correctly. If this is not a common symbol, the usual addend
  486. calculation is done, except that an additional tweak is needed for
  487. PC relative relocs.
  488. FIXME: This macro refers to symbols and asect; these are from the
  489. calling function, not the macro arguments. */
  490. #define CALC_ADDEND(abfd, ptr, reloc, cache_ptr) \
  491. { \
  492. coff_symbol_type *coffsym = NULL; \
  493. \
  494. if (ptr && bfd_asymbol_bfd (ptr) != abfd) \
  495. coffsym = (obj_symbols (abfd) \
  496. + (cache_ptr->sym_ptr_ptr - symbols)); \
  497. else if (ptr) \
  498. coffsym = coff_symbol_from (ptr); \
  499. \
  500. if (coffsym != NULL \
  501. && coffsym->native->u.syment.n_scnum == 0) \
  502. cache_ptr->addend = - coffsym->native->u.syment.n_value; \
  503. else if (ptr && bfd_asymbol_bfd (ptr) == abfd \
  504. && ptr->section != NULL) \
  505. cache_ptr->addend = - (ptr->section->vma + ptr->value); \
  506. else \
  507. cache_ptr->addend = 0; \
  508. if (ptr && reloc.r_type < NUM_HOWTOS \
  509. && howto_table[reloc.r_type].pc_relative) \
  510. cache_ptr->addend += asect->vma; \
  511. }
  512. /* We use the special COFF backend linker. For normal AMD64 COFF, we
  513. can use the generic relocate_section routine. For PE, we need our
  514. own routine. */
  515. #if !defined(COFF_WITH_PE)
  516. #define coff_relocate_section _bfd_coff_generic_relocate_section
  517. #else /* COFF_WITH_PE */
  518. /* The PE relocate section routine. We handle secidx relocations here,
  519. as well as making sure that we don't do anything for a relocatable
  520. link. */
  521. static bool
  522. coff_pe_amd64_relocate_section (bfd *output_bfd,
  523. struct bfd_link_info *info,
  524. bfd *input_bfd,
  525. asection *input_section,
  526. bfd_byte *contents,
  527. struct internal_reloc *relocs,
  528. struct internal_syment *syms,
  529. asection **sections)
  530. {
  531. struct internal_reloc *rel;
  532. struct internal_reloc *relend;
  533. if (bfd_link_relocatable (info))
  534. return true;
  535. rel = relocs;
  536. relend = rel + input_section->reloc_count;
  537. for (; rel < relend; rel++)
  538. {
  539. long symndx;
  540. struct coff_link_hash_entry *h;
  541. asection *sec, *s;
  542. uint16_t idx = 0, i = 1;
  543. if (rel->r_type != R_SECTION)
  544. continue;
  545. /* Make sure that _bfd_coff_generic_relocate_section won't parse
  546. this reloc after us. */
  547. rel->r_type = 0;
  548. symndx = rel->r_symndx;
  549. if (symndx < 0
  550. || (unsigned long) symndx >= obj_raw_syment_count (input_bfd))
  551. continue;
  552. h = obj_coff_sym_hashes (input_bfd)[symndx];
  553. if (h == NULL)
  554. sec = sections[symndx];
  555. else
  556. {
  557. if (h->root.type == bfd_link_hash_defined
  558. || h->root.type == bfd_link_hash_defweak)
  559. {
  560. /* Defined weak symbols are a GNU extension. */
  561. sec = h->root.u.def.section;
  562. }
  563. else
  564. {
  565. sec = NULL;
  566. }
  567. }
  568. if (!sec)
  569. continue;
  570. if (bfd_is_abs_section (sec))
  571. continue;
  572. if (discarded_section (sec))
  573. continue;
  574. s = output_bfd->sections;
  575. while (s)
  576. {
  577. if (s == sec->output_section)
  578. {
  579. idx = i;
  580. break;
  581. }
  582. i++;
  583. s = s->next;
  584. }
  585. bfd_putl16 (idx, contents + rel->r_vaddr - input_section->vma);
  586. }
  587. return _bfd_coff_generic_relocate_section (output_bfd, info, input_bfd,input_section, contents,relocs, syms, sections);
  588. }
  589. #define coff_relocate_section coff_pe_amd64_relocate_section
  590. #endif /* COFF_WITH_PE */
  591. /* Convert an rtype to howto for the COFF backend linker. */
  592. static reloc_howto_type *
  593. coff_amd64_rtype_to_howto (bfd *abfd ATTRIBUTE_UNUSED,
  594. asection *sec,
  595. struct internal_reloc *rel,
  596. struct coff_link_hash_entry *h,
  597. struct internal_syment *sym,
  598. bfd_vma *addendp)
  599. {
  600. reloc_howto_type *howto;
  601. if (rel->r_type >= NUM_HOWTOS)
  602. {
  603. bfd_set_error (bfd_error_bad_value);
  604. return NULL;
  605. }
  606. howto = howto_table + rel->r_type;
  607. #if defined(COFF_WITH_PE)
  608. /* Cancel out code in _bfd_coff_generic_relocate_section. */
  609. *addendp = 0;
  610. if (rel->r_type >= R_AMD64_PCRLONG_1 && rel->r_type <= R_AMD64_PCRLONG_5)
  611. {
  612. *addendp -= (bfd_vma)(rel->r_type - R_AMD64_PCRLONG);
  613. rel->r_type = R_AMD64_PCRLONG;
  614. }
  615. #endif
  616. if (howto->pc_relative)
  617. *addendp += sec->vma;
  618. if (sym != NULL && sym->n_scnum == 0 && sym->n_value != 0)
  619. {
  620. /* This is a common symbol. The section contents include the
  621. size (sym->n_value) as an addend. The relocate_section
  622. function will be adding in the final value of the symbol. We
  623. need to subtract out the current size in order to get the
  624. correct result. */
  625. BFD_ASSERT (h != NULL);
  626. #if !defined(COFF_WITH_PE)
  627. /* I think we *do* want to bypass this. If we don't, I have
  628. seen some data parameters get the wrong relocation address.
  629. If I link two versions with and without this section bypassed
  630. and then do a binary comparison, the addresses which are
  631. different can be looked up in the map. The case in which
  632. this section has been bypassed has addresses which correspond
  633. to values I can find in the map. */
  634. *addendp -= sym->n_value;
  635. #endif
  636. }
  637. #if !defined(COFF_WITH_PE)
  638. /* If the output symbol is common (in which case this must be a
  639. relocatable link), we need to add in the final size of the
  640. common symbol. */
  641. if (h != NULL && h->root.type == bfd_link_hash_common)
  642. *addendp += h->root.u.c.size;
  643. #endif
  644. #if defined(COFF_WITH_PE)
  645. if (howto->pc_relative)
  646. {
  647. #ifndef DONT_EXTEND_AMD64
  648. if (rel->r_type == R_AMD64_PCRQUAD)
  649. *addendp -= 8;
  650. else
  651. #endif
  652. *addendp -= 4;
  653. /* If the symbol is defined, then the generic code is going to
  654. add back the symbol value in order to cancel out an
  655. adjustment it made to the addend. However, we set the addend
  656. to 0 at the start of this function. We need to adjust here,
  657. to avoid the adjustment the generic code will make. FIXME:
  658. This is getting a bit hackish. */
  659. if (sym != NULL && sym->n_scnum != 0)
  660. *addendp -= sym->n_value;
  661. }
  662. if (rel->r_type == R_AMD64_IMAGEBASE
  663. && (bfd_get_flavour (sec->output_section->owner) == bfd_target_coff_flavour))
  664. *addendp -= pe_data (sec->output_section->owner)->pe_opthdr.ImageBase;
  665. if (rel->r_type == R_AMD64_SECREL)
  666. {
  667. bfd_vma osect_vma;
  668. if (h && (h->root.type == bfd_link_hash_defined
  669. || h->root.type == bfd_link_hash_defweak))
  670. osect_vma = h->root.u.def.section->output_section->vma;
  671. else
  672. {
  673. asection *s;
  674. int i;
  675. /* Sigh, the only way to get the section to offset against
  676. is to find it the hard way. */
  677. for (s = abfd->sections, i = 1; i < sym->n_scnum; i++)
  678. s = s->next;
  679. osect_vma = s->output_section->vma;
  680. }
  681. *addendp -= osect_vma;
  682. }
  683. #endif
  684. return howto;
  685. }
  686. #define coff_bfd_reloc_type_lookup coff_amd64_reloc_type_lookup
  687. #define coff_bfd_reloc_name_lookup coff_amd64_reloc_name_lookup
  688. static reloc_howto_type *
  689. coff_amd64_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED, bfd_reloc_code_real_type code)
  690. {
  691. switch (code)
  692. {
  693. case BFD_RELOC_RVA:
  694. return howto_table + R_AMD64_IMAGEBASE;
  695. case BFD_RELOC_32:
  696. return howto_table + R_AMD64_DIR32;
  697. case BFD_RELOC_64:
  698. return howto_table + R_AMD64_DIR64;
  699. case BFD_RELOC_64_PCREL:
  700. #ifndef DONT_EXTEND_AMD64
  701. return howto_table + R_AMD64_PCRQUAD;
  702. #else
  703. /* Fall through. */
  704. #endif
  705. case BFD_RELOC_32_PCREL:
  706. return howto_table + R_AMD64_PCRLONG;
  707. case BFD_RELOC_X86_64_32S:
  708. return howto_table + R_RELLONG;
  709. case BFD_RELOC_16:
  710. return howto_table + R_RELWORD;
  711. case BFD_RELOC_16_PCREL:
  712. return howto_table + R_PCRWORD;
  713. case BFD_RELOC_8:
  714. return howto_table + R_RELBYTE;
  715. case BFD_RELOC_8_PCREL:
  716. return howto_table + R_PCRBYTE;
  717. #if defined(COFF_WITH_PE)
  718. case BFD_RELOC_32_SECREL:
  719. return howto_table + R_AMD64_SECREL;
  720. case BFD_RELOC_16_SECIDX:
  721. return howto_table + R_AMD64_SECTION;
  722. #endif
  723. default:
  724. BFD_FAIL ();
  725. return 0;
  726. }
  727. }
  728. static reloc_howto_type *
  729. coff_amd64_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
  730. const char *r_name)
  731. {
  732. unsigned int i;
  733. for (i = 0; i < NUM_HOWTOS; i++)
  734. if (howto_table[i].name != NULL
  735. && strcasecmp (howto_table[i].name, r_name) == 0)
  736. return &howto_table[i];
  737. return NULL;
  738. }
  739. #define coff_rtype_to_howto coff_amd64_rtype_to_howto
  740. #ifdef TARGET_UNDERSCORE
  741. /* If amd64 gcc uses underscores for symbol names, then it does not use
  742. a leading dot for local labels, so if TARGET_UNDERSCORE is defined
  743. we treat all symbols starting with L as local. */
  744. static bool
  745. coff_amd64_is_local_label_name (bfd *abfd, const char *name)
  746. {
  747. if (name[0] == 'L')
  748. return true;
  749. return _bfd_coff_is_local_label_name (abfd, name);
  750. }
  751. #define coff_bfd_is_local_label_name coff_amd64_is_local_label_name
  752. #endif /* TARGET_UNDERSCORE */
  753. #ifndef bfd_pe_print_pdata
  754. #define bfd_pe_print_pdata NULL
  755. #endif
  756. #include "coffcode.h"
  757. #ifdef PE
  758. #define amd64coff_object_p pe_bfd_object_p
  759. #else
  760. #define amd64coff_object_p coff_object_p
  761. #endif
  762. const bfd_target
  763. #ifdef TARGET_SYM
  764. TARGET_SYM =
  765. #else
  766. x86_64_coff_vec =
  767. #endif
  768. {
  769. #ifdef TARGET_NAME
  770. TARGET_NAME,
  771. #else
  772. "coff-x86-64", /* Name. */
  773. #endif
  774. bfd_target_coff_flavour,
  775. BFD_ENDIAN_LITTLE, /* Data byte order is little. */
  776. BFD_ENDIAN_LITTLE, /* Header byte order is little. */
  777. (HAS_RELOC | EXEC_P /* Object flags. */
  778. | HAS_LINENO | HAS_DEBUG
  779. | HAS_SYMS | HAS_LOCALS | WP_TEXT | D_PAGED | BFD_COMPRESS | BFD_DECOMPRESS),
  780. (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC /* Section flags. */
  781. #if defined(COFF_WITH_PE)
  782. | SEC_LINK_ONCE | SEC_LINK_DUPLICATES | SEC_READONLY | SEC_DEBUGGING
  783. #endif
  784. | SEC_CODE | SEC_DATA | SEC_EXCLUDE ),
  785. #ifdef TARGET_UNDERSCORE
  786. TARGET_UNDERSCORE, /* Leading underscore. */
  787. #else
  788. 0, /* Leading underscore. */
  789. #endif
  790. '/', /* Ar_pad_char. */
  791. 15, /* Ar_max_namelen. */
  792. 0, /* match priority. */
  793. TARGET_KEEP_UNUSED_SECTION_SYMBOLS, /* keep unused section symbols. */
  794. bfd_getl64, bfd_getl_signed_64, bfd_putl64,
  795. bfd_getl32, bfd_getl_signed_32, bfd_putl32,
  796. bfd_getl16, bfd_getl_signed_16, bfd_putl16, /* Data. */
  797. bfd_getl64, bfd_getl_signed_64, bfd_putl64,
  798. bfd_getl32, bfd_getl_signed_32, bfd_putl32,
  799. bfd_getl16, bfd_getl_signed_16, bfd_putl16, /* Hdrs. */
  800. /* Note that we allow an object file to be treated as a core file as well. */
  801. { /* bfd_check_format. */
  802. _bfd_dummy_target,
  803. amd64coff_object_p,
  804. bfd_generic_archive_p,
  805. amd64coff_object_p
  806. },
  807. { /* bfd_set_format. */
  808. _bfd_bool_bfd_false_error,
  809. coff_mkobject,
  810. _bfd_generic_mkarchive,
  811. _bfd_bool_bfd_false_error
  812. },
  813. { /* bfd_write_contents. */
  814. _bfd_bool_bfd_false_error,
  815. coff_write_object_contents,
  816. _bfd_write_archive_contents,
  817. _bfd_bool_bfd_false_error
  818. },
  819. BFD_JUMP_TABLE_GENERIC (coff),
  820. BFD_JUMP_TABLE_COPY (coff),
  821. BFD_JUMP_TABLE_CORE (_bfd_nocore),
  822. BFD_JUMP_TABLE_ARCHIVE (_bfd_archive_coff),
  823. BFD_JUMP_TABLE_SYMBOLS (coff),
  824. BFD_JUMP_TABLE_RELOCS (coff),
  825. BFD_JUMP_TABLE_WRITE (coff),
  826. BFD_JUMP_TABLE_LINK (coff),
  827. BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
  828. NULL,
  829. COFF_SWAP_TABLE
  830. };
  831. /* Entry for big object files. */
  832. #ifdef COFF_WITH_PE_BIGOBJ
  833. const bfd_target
  834. TARGET_SYM_BIG =
  835. {
  836. TARGET_NAME_BIG,
  837. bfd_target_coff_flavour,
  838. BFD_ENDIAN_LITTLE, /* Data byte order is little. */
  839. BFD_ENDIAN_LITTLE, /* Header byte order is little. */
  840. (HAS_RELOC | EXEC_P /* Object flags. */
  841. | HAS_LINENO | HAS_DEBUG
  842. | HAS_SYMS | HAS_LOCALS | WP_TEXT | D_PAGED | BFD_COMPRESS | BFD_DECOMPRESS),
  843. (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC /* Section flags. */
  844. #if defined(COFF_WITH_PE)
  845. | SEC_LINK_ONCE | SEC_LINK_DUPLICATES | SEC_READONLY | SEC_DEBUGGING
  846. #endif
  847. | SEC_CODE | SEC_DATA | SEC_EXCLUDE ),
  848. #ifdef TARGET_UNDERSCORE
  849. TARGET_UNDERSCORE, /* Leading underscore. */
  850. #else
  851. 0, /* Leading underscore. */
  852. #endif
  853. '/', /* Ar_pad_char. */
  854. 15, /* Ar_max_namelen. */
  855. 0, /* match priority. */
  856. TARGET_KEEP_UNUSED_SECTION_SYMBOLS, /* keep unused section symbols. */
  857. bfd_getl64, bfd_getl_signed_64, bfd_putl64,
  858. bfd_getl32, bfd_getl_signed_32, bfd_putl32,
  859. bfd_getl16, bfd_getl_signed_16, bfd_putl16, /* Data. */
  860. bfd_getl64, bfd_getl_signed_64, bfd_putl64,
  861. bfd_getl32, bfd_getl_signed_32, bfd_putl32,
  862. bfd_getl16, bfd_getl_signed_16, bfd_putl16, /* Hdrs. */
  863. /* Note that we allow an object file to be treated as a core file as well. */
  864. { /* bfd_check_format. */
  865. _bfd_dummy_target,
  866. amd64coff_object_p,
  867. bfd_generic_archive_p,
  868. amd64coff_object_p
  869. },
  870. { /* bfd_set_format. */
  871. _bfd_bool_bfd_false_error,
  872. coff_mkobject,
  873. _bfd_generic_mkarchive,
  874. _bfd_bool_bfd_false_error
  875. },
  876. { /* bfd_write_contents. */
  877. _bfd_bool_bfd_false_error,
  878. coff_write_object_contents,
  879. _bfd_write_archive_contents,
  880. _bfd_bool_bfd_false_error
  881. },
  882. BFD_JUMP_TABLE_GENERIC (coff),
  883. BFD_JUMP_TABLE_COPY (coff),
  884. BFD_JUMP_TABLE_CORE (_bfd_nocore),
  885. BFD_JUMP_TABLE_ARCHIVE (_bfd_archive_coff),
  886. BFD_JUMP_TABLE_SYMBOLS (coff),
  887. BFD_JUMP_TABLE_RELOCS (coff),
  888. BFD_JUMP_TABLE_WRITE (coff),
  889. BFD_JUMP_TABLE_LINK (coff),
  890. BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
  891. NULL,
  892. &bigobj_swap_table
  893. };
  894. #endif