coff-i386.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864
  1. /* BFD back-end for Intel 386 COFF files.
  2. Copyright (C) 1990-2022 Free Software Foundation, Inc.
  3. Written by Cygnus Support.
  4. This file is part of BFD, the Binary File Descriptor library.
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 3 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
  16. MA 02110-1301, USA. */
  17. #include "sysdep.h"
  18. #include "bfd.h"
  19. #include "libbfd.h"
  20. #include "coff/i386.h"
  21. #include "coff/internal.h"
  22. #ifdef COFF_WITH_PE
  23. #include "coff/pe.h"
  24. #endif
  25. #ifndef bfd_pe_print_pdata
  26. #define bfd_pe_print_pdata NULL
  27. #endif
  28. #include "libcoff.h"
  29. /* All users of this file have bfd_octets_per_byte (abfd, sec) == 1. */
  30. #define OCTETS_PER_BYTE(ABFD, SEC) 1
  31. static reloc_howto_type *coff_i386_rtype_to_howto
  32. (bfd *, asection *, struct internal_reloc *,
  33. struct coff_link_hash_entry *, struct internal_syment *,
  34. bfd_vma *);
  35. static reloc_howto_type *coff_i386_reloc_type_lookup
  36. (bfd *, bfd_reloc_code_real_type);
  37. #define COFF_DEFAULT_SECTION_ALIGNMENT_POWER (2)
  38. /* The page size is a guess based on ELF. */
  39. #define COFF_PAGE_SIZE 0x1000
  40. /* For some reason when using i386 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_i386_reloc (bfd *abfd,
  49. arelent *reloc_entry,
  50. asymbol *symbol,
  51. void * data,
  52. asection *input_section,
  53. bfd *output_bfd,
  54. char **error_message ATTRIBUTE_UNUSED)
  55. {
  56. symvalue diff;
  57. #ifndef COFF_WITH_PE
  58. if (output_bfd == (bfd *) NULL)
  59. return bfd_reloc_continue;
  60. #endif
  61. if (bfd_is_com_section (symbol->section))
  62. {
  63. #ifndef 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. #ifdef COFF_WITH_PE
  88. if (output_bfd == (bfd *) NULL)
  89. {
  90. reloc_howto_type *howto = reloc_entry->howto;
  91. /* Although PC relative relocations are very similar between
  92. PE and non-PE formats, but they are off by 1 << howto->size
  93. bytes. For the external relocation, PE is very different
  94. from others. See md_apply_fix3 () in gas/config/tc-i386.c.
  95. When we link PE and non-PE object files together to
  96. generate a non-PE executable, we have to compensate it
  97. here. */
  98. if (howto->pc_relative && howto->pcrel_offset)
  99. diff = -(1 << howto->size);
  100. else if (symbol->flags & BSF_WEAK)
  101. diff = reloc_entry->addend - symbol->value;
  102. else
  103. diff = -reloc_entry->addend;
  104. }
  105. else
  106. #endif
  107. diff = reloc_entry->addend;
  108. }
  109. #ifdef COFF_WITH_PE
  110. /* FIXME: How should this case be handled? */
  111. if (reloc_entry->howto->type == R_IMAGEBASE
  112. && output_bfd != NULL
  113. && bfd_get_flavour(output_bfd) == bfd_target_coff_flavour)
  114. diff -= pe_data (output_bfd)->pe_opthdr.ImageBase;
  115. #endif
  116. #define DOIT(x) \
  117. x = ((x & ~howto->dst_mask) | (((x & howto->src_mask) + diff) & howto->dst_mask))
  118. if (diff != 0)
  119. {
  120. reloc_howto_type *howto = reloc_entry->howto;
  121. bfd_size_type octets = (reloc_entry->address
  122. * OCTETS_PER_BYTE (abfd, input_section));
  123. unsigned char *addr = (unsigned char *) data + octets;
  124. if (!bfd_reloc_offset_in_range (howto, abfd, input_section, octets))
  125. return bfd_reloc_outofrange;
  126. switch (howto->size)
  127. {
  128. case 0:
  129. {
  130. char x = bfd_get_8 (abfd, addr);
  131. DOIT (x);
  132. bfd_put_8 (abfd, x, addr);
  133. }
  134. break;
  135. case 1:
  136. {
  137. short x = bfd_get_16 (abfd, addr);
  138. DOIT (x);
  139. bfd_put_16 (abfd, (bfd_vma) x, addr);
  140. }
  141. break;
  142. case 2:
  143. {
  144. long x = bfd_get_32 (abfd, addr);
  145. DOIT (x);
  146. bfd_put_32 (abfd, (bfd_vma) x, addr);
  147. }
  148. break;
  149. default:
  150. abort ();
  151. }
  152. }
  153. /* Now let bfd_perform_relocation finish everything up. */
  154. return bfd_reloc_continue;
  155. }
  156. #ifdef COFF_WITH_PE
  157. /* Return TRUE if this relocation should appear in the output .reloc
  158. section. */
  159. static bool
  160. in_reloc_p (bfd *abfd ATTRIBUTE_UNUSED, reloc_howto_type *howto)
  161. {
  162. return ! howto->pc_relative
  163. && howto->type != R_IMAGEBASE
  164. && howto->type != R_SECREL32
  165. && howto->type != R_SECTION;
  166. }
  167. #endif /* COFF_WITH_PE */
  168. #ifndef PCRELOFFSET
  169. #define PCRELOFFSET false
  170. #endif
  171. static reloc_howto_type howto_table[] =
  172. {
  173. EMPTY_HOWTO (0),
  174. EMPTY_HOWTO (1),
  175. EMPTY_HOWTO (2),
  176. EMPTY_HOWTO (3),
  177. EMPTY_HOWTO (4),
  178. EMPTY_HOWTO (5),
  179. HOWTO (R_DIR32, /* type */
  180. 0, /* rightshift */
  181. 2, /* size (0 = byte, 1 = short, 2 = long) */
  182. 32, /* bitsize */
  183. false, /* pc_relative */
  184. 0, /* bitpos */
  185. complain_overflow_bitfield, /* complain_on_overflow */
  186. coff_i386_reloc, /* special_function */
  187. "dir32", /* name */
  188. true, /* partial_inplace */
  189. 0xffffffff, /* src_mask */
  190. 0xffffffff, /* dst_mask */
  191. true), /* pcrel_offset */
  192. /* PE IMAGE_REL_I386_DIR32NB relocation (7). */
  193. HOWTO (R_IMAGEBASE, /* type */
  194. 0, /* rightshift */
  195. 2, /* size (0 = byte, 1 = short, 2 = long) */
  196. 32, /* bitsize */
  197. false, /* pc_relative */
  198. 0, /* bitpos */
  199. complain_overflow_bitfield, /* complain_on_overflow */
  200. coff_i386_reloc, /* special_function */
  201. "rva32", /* name */
  202. true, /* partial_inplace */
  203. 0xffffffff, /* src_mask */
  204. 0xffffffff, /* dst_mask */
  205. false), /* pcrel_offset */
  206. EMPTY_HOWTO (010),
  207. EMPTY_HOWTO (011),
  208. #ifdef COFF_WITH_PE
  209. /* 16-bit word section relocation (012). */
  210. HOWTO (R_SECTION, /* type */
  211. 0, /* rightshift */
  212. 1, /* size (0 = byte, 1 = short, 2 = long) */
  213. 16, /* bitsize */
  214. false, /* pc_relative */
  215. 0, /* bitpos */
  216. complain_overflow_bitfield, /* complain_on_overflow */
  217. coff_i386_reloc, /* special_function */
  218. "secidx", /* name */
  219. true, /* partial_inplace */
  220. 0xffffffff, /* src_mask */
  221. 0xffffffff, /* dst_mask */
  222. true), /* pcrel_offset */
  223. /* 32-bit longword section relative relocation (013). */
  224. HOWTO (R_SECREL32, /* type */
  225. 0, /* rightshift */
  226. 2, /* size (0 = byte, 1 = short, 2 = long) */
  227. 32, /* bitsize */
  228. false, /* pc_relative */
  229. 0, /* bitpos */
  230. complain_overflow_bitfield, /* complain_on_overflow */
  231. coff_i386_reloc, /* special_function */
  232. "secrel32", /* name */
  233. true, /* partial_inplace */
  234. 0xffffffff, /* src_mask */
  235. 0xffffffff, /* dst_mask */
  236. true), /* pcrel_offset */
  237. #else
  238. EMPTY_HOWTO (012),
  239. EMPTY_HOWTO (013),
  240. #endif
  241. EMPTY_HOWTO (014),
  242. EMPTY_HOWTO (015),
  243. EMPTY_HOWTO (016),
  244. /* Byte relocation (017). */
  245. HOWTO (R_RELBYTE, /* type */
  246. 0, /* rightshift */
  247. 0, /* size (0 = byte, 1 = short, 2 = long) */
  248. 8, /* bitsize */
  249. false, /* pc_relative */
  250. 0, /* bitpos */
  251. complain_overflow_bitfield, /* complain_on_overflow */
  252. coff_i386_reloc, /* special_function */
  253. "8", /* name */
  254. true, /* partial_inplace */
  255. 0x000000ff, /* src_mask */
  256. 0x000000ff, /* dst_mask */
  257. PCRELOFFSET), /* pcrel_offset */
  258. /* 16-bit word relocation (020). */
  259. HOWTO (R_RELWORD, /* type */
  260. 0, /* rightshift */
  261. 1, /* size (0 = byte, 1 = short, 2 = long) */
  262. 16, /* bitsize */
  263. false, /* pc_relative */
  264. 0, /* bitpos */
  265. complain_overflow_bitfield, /* complain_on_overflow */
  266. coff_i386_reloc, /* special_function */
  267. "16", /* name */
  268. true, /* partial_inplace */
  269. 0x0000ffff, /* src_mask */
  270. 0x0000ffff, /* dst_mask */
  271. PCRELOFFSET), /* pcrel_offset */
  272. /* 32-bit longword relocation (021). */
  273. HOWTO (R_RELLONG, /* type */
  274. 0, /* rightshift */
  275. 2, /* size (0 = byte, 1 = short, 2 = long) */
  276. 32, /* bitsize */
  277. false, /* pc_relative */
  278. 0, /* bitpos */
  279. complain_overflow_bitfield, /* complain_on_overflow */
  280. coff_i386_reloc, /* special_function */
  281. "32", /* name */
  282. true, /* partial_inplace */
  283. 0xffffffff, /* src_mask */
  284. 0xffffffff, /* dst_mask */
  285. PCRELOFFSET), /* pcrel_offset */
  286. /* Byte PC relative relocation (022). */
  287. HOWTO (R_PCRBYTE, /* type */
  288. 0, /* rightshift */
  289. 0, /* size (0 = byte, 1 = short, 2 = long) */
  290. 8, /* bitsize */
  291. true, /* pc_relative */
  292. 0, /* bitpos */
  293. complain_overflow_signed, /* complain_on_overflow */
  294. coff_i386_reloc, /* special_function */
  295. "DISP8", /* name */
  296. true, /* partial_inplace */
  297. 0x000000ff, /* src_mask */
  298. 0x000000ff, /* dst_mask */
  299. PCRELOFFSET), /* pcrel_offset */
  300. /* 16-bit word PC relative relocation (023). */
  301. HOWTO (R_PCRWORD, /* type */
  302. 0, /* rightshift */
  303. 1, /* size (0 = byte, 1 = short, 2 = long) */
  304. 16, /* bitsize */
  305. true, /* pc_relative */
  306. 0, /* bitpos */
  307. complain_overflow_signed, /* complain_on_overflow */
  308. coff_i386_reloc, /* special_function */
  309. "DISP16", /* name */
  310. true, /* partial_inplace */
  311. 0x0000ffff, /* src_mask */
  312. 0x0000ffff, /* dst_mask */
  313. PCRELOFFSET), /* pcrel_offset */
  314. /* 32-bit longword PC relative relocation (024). */
  315. HOWTO (R_PCRLONG, /* type */
  316. 0, /* rightshift */
  317. 2, /* size (0 = byte, 1 = short, 2 = long) */
  318. 32, /* bitsize */
  319. true, /* pc_relative */
  320. 0, /* bitpos */
  321. complain_overflow_signed, /* complain_on_overflow */
  322. coff_i386_reloc, /* special_function */
  323. "DISP32", /* name */
  324. true, /* partial_inplace */
  325. 0xffffffff, /* src_mask */
  326. 0xffffffff, /* dst_mask */
  327. PCRELOFFSET) /* pcrel_offset */
  328. };
  329. #define NUM_HOWTOS (sizeof (howto_table) / sizeof (howto_table[0]))
  330. /* Turn a howto into a reloc nunmber */
  331. #define SELECT_RELOC(x,howto) { x.r_type = howto->type; }
  332. #define BADMAG(x) I386BADMAG(x)
  333. #define I386 1 /* Customize coffcode.h */
  334. #define RTYPE2HOWTO(cache_ptr, dst) \
  335. ((cache_ptr)->howto = \
  336. ((dst)->r_type < NUM_HOWTOS \
  337. ? howto_table + (dst)->r_type \
  338. : NULL))
  339. /* For 386 COFF a STYP_NOLOAD | STYP_BSS section is part of a shared
  340. library. On some other COFF targets STYP_BSS is normally
  341. STYP_NOLOAD. */
  342. #define BSS_NOLOAD_IS_SHARED_LIBRARY
  343. /* Compute the addend of a reloc. If the reloc is to a common symbol,
  344. the object file contains the value of the common symbol. By the
  345. time this is called, the linker may be using a different symbol
  346. from a different object file with a different value. Therefore, we
  347. hack wildly to locate the original symbol from this file so that we
  348. can make the correct adjustment. This macro sets coffsym to the
  349. symbol from the original file, and uses it to set the addend value
  350. correctly. If this is not a common symbol, the usual addend
  351. calculation is done, except that an additional tweak is needed for
  352. PC relative relocs.
  353. FIXME: This macro refers to symbols and asect; these are from the
  354. calling function, not the macro arguments. */
  355. #define CALC_ADDEND(abfd, ptr, reloc, cache_ptr) \
  356. { \
  357. coff_symbol_type *coffsym = (coff_symbol_type *) NULL; \
  358. if (ptr && bfd_asymbol_bfd (ptr) != abfd) \
  359. coffsym = (obj_symbols (abfd) \
  360. + (cache_ptr->sym_ptr_ptr - symbols)); \
  361. else if (ptr) \
  362. coffsym = coff_symbol_from (ptr); \
  363. if (coffsym != (coff_symbol_type *) NULL \
  364. && coffsym->native->u.syment.n_scnum == 0) \
  365. cache_ptr->addend = - coffsym->native->u.syment.n_value; \
  366. else if (ptr && bfd_asymbol_bfd (ptr) == abfd \
  367. && ptr->section != (asection *) NULL) \
  368. cache_ptr->addend = - (ptr->section->vma + ptr->value); \
  369. else \
  370. cache_ptr->addend = 0; \
  371. if (ptr && reloc.r_type < NUM_HOWTOS \
  372. && howto_table[reloc.r_type].pc_relative) \
  373. cache_ptr->addend += asect->vma; \
  374. }
  375. /* We use the special COFF backend linker. For normal i386 COFF, we
  376. can use the generic relocate_section routine. For PE, we need our
  377. own routine. */
  378. #ifndef COFF_WITH_PE
  379. #define coff_relocate_section _bfd_coff_generic_relocate_section
  380. #else /* COFF_WITH_PE */
  381. /* The PE relocate section routine. We handle secidx relocations here,
  382. as well as making sure that we don't do anything for a relocatable
  383. link. */
  384. static bool
  385. coff_pe_i386_relocate_section (bfd *output_bfd,
  386. struct bfd_link_info *info,
  387. bfd *input_bfd,
  388. asection *input_section,
  389. bfd_byte *contents,
  390. struct internal_reloc *relocs,
  391. struct internal_syment *syms,
  392. asection **sections)
  393. {
  394. struct internal_reloc *rel;
  395. struct internal_reloc *relend;
  396. if (bfd_link_relocatable (info))
  397. return true;
  398. rel = relocs;
  399. relend = rel + input_section->reloc_count;
  400. for (; rel < relend; rel++)
  401. {
  402. long symndx;
  403. struct coff_link_hash_entry *h;
  404. asection *sec, *s;
  405. uint16_t idx = 0, i = 1;
  406. if (rel->r_type != R_SECTION)
  407. continue;
  408. /* Make sure that _bfd_coff_generic_relocate_section won't parse
  409. this reloc after us. */
  410. rel->r_type = 0;
  411. symndx = rel->r_symndx;
  412. if (symndx < 0
  413. || (unsigned long) symndx >= obj_raw_syment_count (input_bfd))
  414. continue;
  415. h = obj_coff_sym_hashes (input_bfd)[symndx];
  416. if (h == NULL)
  417. sec = sections[symndx];
  418. else
  419. {
  420. if (h->root.type == bfd_link_hash_defined
  421. || h->root.type == bfd_link_hash_defweak)
  422. {
  423. /* Defined weak symbols are a GNU extension. */
  424. sec = h->root.u.def.section;
  425. }
  426. else
  427. {
  428. sec = NULL;
  429. }
  430. }
  431. if (!sec)
  432. continue;
  433. if (bfd_is_abs_section (sec))
  434. continue;
  435. if (discarded_section (sec))
  436. continue;
  437. s = output_bfd->sections;
  438. while (s)
  439. {
  440. if (s == sec->output_section)
  441. {
  442. idx = i;
  443. break;
  444. }
  445. i++;
  446. s = s->next;
  447. }
  448. bfd_putl16 (idx, contents + rel->r_vaddr - input_section->vma);
  449. }
  450. return _bfd_coff_generic_relocate_section (output_bfd, info, input_bfd,
  451. input_section, contents,
  452. relocs, syms, sections);
  453. }
  454. #define coff_relocate_section coff_pe_i386_relocate_section
  455. #endif /* COFF_WITH_PE */
  456. /* Convert an rtype to howto for the COFF backend linker. */
  457. static reloc_howto_type *
  458. coff_i386_rtype_to_howto (bfd *abfd ATTRIBUTE_UNUSED,
  459. asection *sec,
  460. struct internal_reloc *rel,
  461. struct coff_link_hash_entry *h,
  462. struct internal_syment *sym,
  463. bfd_vma *addendp)
  464. {
  465. reloc_howto_type *howto;
  466. if (rel->r_type >= NUM_HOWTOS)
  467. {
  468. bfd_set_error (bfd_error_bad_value);
  469. return NULL;
  470. }
  471. howto = howto_table + rel->r_type;
  472. #ifdef COFF_WITH_PE
  473. /* Cancel out code in _bfd_coff_generic_relocate_section. */
  474. *addendp = 0;
  475. #endif
  476. if (howto->pc_relative)
  477. *addendp += sec->vma;
  478. if (sym != NULL && sym->n_scnum == 0 && sym->n_value != 0)
  479. {
  480. /* This is a common symbol. The section contents include the
  481. size (sym->n_value) as an addend. The relocate_section
  482. function will be adding in the final value of the symbol. We
  483. need to subtract out the current size in order to get the
  484. correct result. */
  485. BFD_ASSERT (h != NULL);
  486. #ifndef COFF_WITH_PE
  487. /* I think we *do* want to bypass this. If we don't, I have
  488. seen some data parameters get the wrong relocation address.
  489. If I link two versions with and without this section bypassed
  490. and then do a binary comparison, the addresses which are
  491. different can be looked up in the map. The case in which
  492. this section has been bypassed has addresses which correspond
  493. to values I can find in the map. */
  494. *addendp -= sym->n_value;
  495. #endif
  496. }
  497. #ifndef COFF_WITH_PE
  498. /* If the output symbol is common (in which case this must be a
  499. relocatable link), we need to add in the final size of the
  500. common symbol. */
  501. if (h != NULL && h->root.type == bfd_link_hash_common)
  502. *addendp += h->root.u.c.size;
  503. #endif
  504. #ifdef COFF_WITH_PE
  505. if (howto->pc_relative)
  506. {
  507. *addendp -= 4;
  508. /* If the symbol is defined, then the generic code is going to
  509. add back the symbol value in order to cancel out an
  510. adjustment it made to the addend. However, we set the addend
  511. to 0 at the start of this function. We need to adjust here,
  512. to avoid the adjustment the generic code will make. FIXME:
  513. This is getting a bit hackish. */
  514. if (sym != NULL && sym->n_scnum != 0)
  515. *addendp -= sym->n_value;
  516. }
  517. if (rel->r_type == R_IMAGEBASE
  518. && (bfd_get_flavour(sec->output_section->owner)
  519. == bfd_target_coff_flavour))
  520. {
  521. *addendp -= pe_data(sec->output_section->owner)->pe_opthdr.ImageBase;
  522. }
  523. /* PR 17099 - Absolute R_PCRLONG relocations do not need a symbol. */
  524. if (rel->r_type == R_PCRLONG && sym == NULL)
  525. *addendp -= rel->r_vaddr;
  526. else
  527. BFD_ASSERT (sym != NULL);
  528. if (rel->r_type == R_SECREL32 && sym != NULL)
  529. {
  530. bfd_vma osect_vma;
  531. if (h && (h->root.type == bfd_link_hash_defined
  532. || h->root.type == bfd_link_hash_defweak))
  533. osect_vma = h->root.u.def.section->output_section->vma;
  534. else
  535. {
  536. asection *s;
  537. int i;
  538. /* Sigh, the only way to get the section to offset against
  539. is to find it the hard way. */
  540. for (s = abfd->sections, i = 1; i < sym->n_scnum; i++)
  541. s = s->next;
  542. osect_vma = s->output_section->vma;
  543. }
  544. *addendp -= osect_vma;
  545. }
  546. #endif
  547. return howto;
  548. }
  549. #define coff_bfd_reloc_type_lookup coff_i386_reloc_type_lookup
  550. #define coff_bfd_reloc_name_lookup coff_i386_reloc_name_lookup
  551. static reloc_howto_type *
  552. coff_i386_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
  553. bfd_reloc_code_real_type code)
  554. {
  555. switch (code)
  556. {
  557. case BFD_RELOC_RVA:
  558. return howto_table + R_IMAGEBASE;
  559. case BFD_RELOC_32:
  560. return howto_table + R_DIR32;
  561. case BFD_RELOC_32_PCREL:
  562. return howto_table + R_PCRLONG;
  563. case BFD_RELOC_16:
  564. return howto_table + R_RELWORD;
  565. case BFD_RELOC_16_PCREL:
  566. return howto_table + R_PCRWORD;
  567. case BFD_RELOC_8:
  568. return howto_table + R_RELBYTE;
  569. case BFD_RELOC_8_PCREL:
  570. return howto_table + R_PCRBYTE;
  571. #ifdef COFF_WITH_PE
  572. case BFD_RELOC_32_SECREL:
  573. return howto_table + R_SECREL32;
  574. case BFD_RELOC_16_SECIDX:
  575. return howto_table + R_SECTION;
  576. #endif
  577. default:
  578. BFD_FAIL ();
  579. return 0;
  580. }
  581. }
  582. static reloc_howto_type *
  583. coff_i386_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
  584. const char *r_name)
  585. {
  586. unsigned int i;
  587. for (i = 0; i < NUM_HOWTOS; i++)
  588. if (howto_table[i].name != NULL
  589. && strcasecmp (howto_table[i].name, r_name) == 0)
  590. return &howto_table[i];
  591. return NULL;
  592. }
  593. #define coff_rtype_to_howto coff_i386_rtype_to_howto
  594. #ifdef TARGET_UNDERSCORE
  595. /* If i386 gcc uses underscores for symbol names, then it does not use
  596. a leading dot for local labels, so if TARGET_UNDERSCORE is defined
  597. we treat all symbols starting with L as local. */
  598. static bool
  599. coff_i386_is_local_label_name (bfd *abfd, const char *name)
  600. {
  601. if (name[0] == 'L')
  602. return true;
  603. return _bfd_coff_is_local_label_name (abfd, name);
  604. }
  605. #define coff_bfd_is_local_label_name coff_i386_is_local_label_name
  606. #endif /* TARGET_UNDERSCORE */
  607. #include "coffcode.h"
  608. const bfd_target
  609. #ifdef TARGET_SYM
  610. TARGET_SYM =
  611. #else
  612. i386_coff_vec =
  613. #endif
  614. {
  615. #ifdef TARGET_NAME
  616. TARGET_NAME,
  617. #else
  618. "coff-i386", /* name */
  619. #endif
  620. bfd_target_coff_flavour,
  621. BFD_ENDIAN_LITTLE, /* data byte order is little */
  622. BFD_ENDIAN_LITTLE, /* header byte order is little */
  623. (HAS_RELOC | EXEC_P | /* object flags */
  624. HAS_LINENO | HAS_DEBUG |
  625. HAS_SYMS | HAS_LOCALS | WP_TEXT | D_PAGED | BFD_COMPRESS | BFD_DECOMPRESS ),
  626. (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC /* section flags */
  627. #ifdef COFF_WITH_PE
  628. | SEC_LINK_ONCE | SEC_LINK_DUPLICATES | SEC_READONLY | SEC_DEBUGGING
  629. #endif
  630. | SEC_CODE | SEC_DATA | SEC_EXCLUDE ),
  631. #ifdef TARGET_UNDERSCORE
  632. TARGET_UNDERSCORE, /* leading underscore */
  633. #else
  634. 0, /* leading underscore */
  635. #endif
  636. '/', /* ar_pad_char */
  637. 15, /* ar_max_namelen */
  638. 0, /* match priority. */
  639. TARGET_KEEP_UNUSED_SECTION_SYMBOLS, /* keep unused section symbols. */
  640. bfd_getl64, bfd_getl_signed_64, bfd_putl64,
  641. bfd_getl32, bfd_getl_signed_32, bfd_putl32,
  642. bfd_getl16, bfd_getl_signed_16, bfd_putl16, /* data */
  643. bfd_getl64, bfd_getl_signed_64, bfd_putl64,
  644. bfd_getl32, bfd_getl_signed_32, bfd_putl32,
  645. bfd_getl16, bfd_getl_signed_16, bfd_putl16, /* hdrs */
  646. #ifndef COFF_CHECK_FORMAT
  647. #define COFF_CHECK_FORMAT coff_object_p
  648. #endif
  649. #ifndef COFF_WRITE_CONTENTS
  650. #define COFF_WRITE_CONTENTS coff_write_object_contents
  651. #endif
  652. /* Note that we allow an object file to be treated as a core file as well. */
  653. { /* bfd_check_format */
  654. _bfd_dummy_target,
  655. COFF_CHECK_FORMAT,
  656. bfd_generic_archive_p,
  657. COFF_CHECK_FORMAT
  658. },
  659. { /* bfd_set_format */
  660. _bfd_bool_bfd_false_error,
  661. coff_mkobject,
  662. _bfd_generic_mkarchive,
  663. _bfd_bool_bfd_false_error
  664. },
  665. { /* bfd_write_contents */
  666. _bfd_bool_bfd_false_error,
  667. COFF_WRITE_CONTENTS,
  668. _bfd_write_archive_contents,
  669. _bfd_bool_bfd_false_error
  670. },
  671. BFD_JUMP_TABLE_GENERIC (coff),
  672. BFD_JUMP_TABLE_COPY (coff),
  673. BFD_JUMP_TABLE_CORE (_bfd_nocore),
  674. BFD_JUMP_TABLE_ARCHIVE (_bfd_archive_coff),
  675. BFD_JUMP_TABLE_SYMBOLS (coff),
  676. BFD_JUMP_TABLE_RELOCS (coff),
  677. BFD_JUMP_TABLE_WRITE (coff),
  678. BFD_JUMP_TABLE_LINK (coff),
  679. BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
  680. NULL,
  681. COFF_SWAP_TABLE
  682. };
  683. #ifdef COFF_WITH_PE_BIGOBJ
  684. const bfd_target
  685. TARGET_SYM_BIG =
  686. {
  687. TARGET_NAME_BIG,
  688. bfd_target_coff_flavour,
  689. BFD_ENDIAN_LITTLE, /* data byte order is little */
  690. BFD_ENDIAN_LITTLE, /* header byte order is little */
  691. (HAS_RELOC | EXEC_P | /* object flags */
  692. HAS_LINENO | HAS_DEBUG |
  693. HAS_SYMS | HAS_LOCALS | WP_TEXT | D_PAGED | BFD_COMPRESS | BFD_DECOMPRESS ),
  694. (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC /* section flags */
  695. #ifdef COFF_WITH_PE
  696. | SEC_LINK_ONCE | SEC_LINK_DUPLICATES | SEC_READONLY | SEC_DEBUGGING
  697. #endif
  698. | SEC_CODE | SEC_DATA | SEC_EXCLUDE ),
  699. #ifdef TARGET_UNDERSCORE
  700. TARGET_UNDERSCORE, /* leading underscore */
  701. #else
  702. 0, /* leading underscore */
  703. #endif
  704. '/', /* ar_pad_char */
  705. 15, /* ar_max_namelen */
  706. 0, /* match priority. */
  707. TARGET_KEEP_UNUSED_SECTION_SYMBOLS, /* keep unused section symbols. */
  708. bfd_getl64, bfd_getl_signed_64, bfd_putl64,
  709. bfd_getl32, bfd_getl_signed_32, bfd_putl32,
  710. bfd_getl16, bfd_getl_signed_16, bfd_putl16, /* data */
  711. bfd_getl64, bfd_getl_signed_64, bfd_putl64,
  712. bfd_getl32, bfd_getl_signed_32, bfd_putl32,
  713. bfd_getl16, bfd_getl_signed_16, bfd_putl16, /* hdrs */
  714. /* Note that we allow an object file to be treated as a core file as well. */
  715. { /* bfd_check_format */
  716. _bfd_dummy_target,
  717. COFF_CHECK_FORMAT,
  718. bfd_generic_archive_p,
  719. COFF_CHECK_FORMAT
  720. },
  721. { /* bfd_set_format */
  722. _bfd_bool_bfd_false_error,
  723. coff_mkobject,
  724. _bfd_generic_mkarchive,
  725. _bfd_bool_bfd_false_error
  726. },
  727. { /* bfd_write_contents */
  728. _bfd_bool_bfd_false_error,
  729. COFF_WRITE_CONTENTS,
  730. _bfd_write_archive_contents,
  731. _bfd_bool_bfd_false_error
  732. },
  733. BFD_JUMP_TABLE_GENERIC (coff),
  734. BFD_JUMP_TABLE_COPY (coff),
  735. BFD_JUMP_TABLE_CORE (_bfd_nocore),
  736. BFD_JUMP_TABLE_ARCHIVE (_bfd_archive_coff),
  737. BFD_JUMP_TABLE_SYMBOLS (coff),
  738. BFD_JUMP_TABLE_RELOCS (coff),
  739. BFD_JUMP_TABLE_WRITE (coff),
  740. BFD_JUMP_TABLE_LINK (coff),
  741. BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
  742. NULL,
  743. &bigobj_swap_table
  744. };
  745. #endif