coff-mips.c 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501
  1. /* BFD back-end for MIPS Extended-Coff files.
  2. Copyright (C) 1990-2015 Free Software Foundation, Inc.
  3. Original version by Per Bothner.
  4. Full support added by Ian Lance Taylor, ian@cygnus.com.
  5. This file is part of BFD, the Binary File Descriptor library.
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 3 of the License, or
  9. (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program; if not, write to the Free Software
  16. Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
  17. MA 02110-1301, USA. */
  18. #include "sysdep.h"
  19. #include "bfd.h"
  20. #include "bfdlink.h"
  21. #include "libbfd.h"
  22. #include "coff/internal.h"
  23. #include "coff/sym.h"
  24. #include "coff/symconst.h"
  25. #include "coff/ecoff.h"
  26. #include "coff/mips.h"
  27. #include "libcoff.h"
  28. #include "libecoff.h"
  29. /* Prototypes for static functions. */
  30. static bfd_reloc_status_type
  31. mips_generic_reloc
  32. (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
  33. static bfd_reloc_status_type
  34. mips_refhi_reloc
  35. (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
  36. static bfd_reloc_status_type
  37. mips_reflo_reloc
  38. (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
  39. static bfd_reloc_status_type
  40. mips_gprel_reloc
  41. (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
  42. /* ECOFF has COFF sections, but the debugging information is stored in
  43. a completely different format. ECOFF targets use some of the
  44. swapping routines from coffswap.h, and some of the generic COFF
  45. routines in coffgen.c, but, unlike the real COFF targets, do not
  46. use coffcode.h itself.
  47. Get the generic COFF swapping routines, except for the reloc,
  48. symbol, and lineno ones. Give them ECOFF names. */
  49. #define MIPSECOFF
  50. #define NO_COFF_RELOCS
  51. #define NO_COFF_SYMBOLS
  52. #define NO_COFF_LINENOS
  53. #define coff_swap_filehdr_in mips_ecoff_swap_filehdr_in
  54. #define coff_swap_filehdr_out mips_ecoff_swap_filehdr_out
  55. #define coff_swap_aouthdr_in mips_ecoff_swap_aouthdr_in
  56. #define coff_swap_aouthdr_out mips_ecoff_swap_aouthdr_out
  57. #define coff_swap_scnhdr_in mips_ecoff_swap_scnhdr_in
  58. #define coff_swap_scnhdr_out mips_ecoff_swap_scnhdr_out
  59. #include "coffswap.h"
  60. /* Get the ECOFF swapping routines. */
  61. #define ECOFF_32
  62. #include "ecoffswap.h"
  63. /* How to process the various relocs types. */
  64. static reloc_howto_type mips_howto_table[] =
  65. {
  66. /* Reloc type 0 is ignored. The reloc reading code ensures that
  67. this is a reference to the .abs section, which will cause
  68. bfd_perform_relocation to do nothing. */
  69. HOWTO (MIPS_R_IGNORE, /* type */
  70. 0, /* rightshift */
  71. 0, /* size (0 = byte, 1 = short, 2 = long) */
  72. 8, /* bitsize */
  73. FALSE, /* pc_relative */
  74. 0, /* bitpos */
  75. complain_overflow_dont, /* complain_on_overflow */
  76. 0, /* special_function */
  77. "IGNORE", /* name */
  78. FALSE, /* partial_inplace */
  79. 0, /* src_mask */
  80. 0, /* dst_mask */
  81. FALSE), /* pcrel_offset */
  82. /* A 16 bit reference to a symbol, normally from a data section. */
  83. HOWTO (MIPS_R_REFHALF, /* type */
  84. 0, /* rightshift */
  85. 1, /* size (0 = byte, 1 = short, 2 = long) */
  86. 16, /* bitsize */
  87. FALSE, /* pc_relative */
  88. 0, /* bitpos */
  89. complain_overflow_bitfield, /* complain_on_overflow */
  90. mips_generic_reloc, /* special_function */
  91. "REFHALF", /* name */
  92. TRUE, /* partial_inplace */
  93. 0xffff, /* src_mask */
  94. 0xffff, /* dst_mask */
  95. FALSE), /* pcrel_offset */
  96. /* A 32 bit reference to a symbol, normally from a data section. */
  97. HOWTO (MIPS_R_REFWORD, /* type */
  98. 0, /* rightshift */
  99. 2, /* size (0 = byte, 1 = short, 2 = long) */
  100. 32, /* bitsize */
  101. FALSE, /* pc_relative */
  102. 0, /* bitpos */
  103. complain_overflow_bitfield, /* complain_on_overflow */
  104. mips_generic_reloc, /* special_function */
  105. "REFWORD", /* name */
  106. TRUE, /* partial_inplace */
  107. 0xffffffff, /* src_mask */
  108. 0xffffffff, /* dst_mask */
  109. FALSE), /* pcrel_offset */
  110. /* A 26 bit absolute jump address. */
  111. HOWTO (MIPS_R_JMPADDR, /* type */
  112. 2, /* rightshift */
  113. 2, /* size (0 = byte, 1 = short, 2 = long) */
  114. 26, /* bitsize */
  115. FALSE, /* pc_relative */
  116. 0, /* bitpos */
  117. complain_overflow_dont, /* complain_on_overflow */
  118. /* This needs complex overflow
  119. detection, because the upper four
  120. bits must match the PC. */
  121. mips_generic_reloc, /* special_function */
  122. "JMPADDR", /* name */
  123. TRUE, /* partial_inplace */
  124. 0x3ffffff, /* src_mask */
  125. 0x3ffffff, /* dst_mask */
  126. FALSE), /* pcrel_offset */
  127. /* The high 16 bits of a symbol value. Handled by the function
  128. mips_refhi_reloc. */
  129. HOWTO (MIPS_R_REFHI, /* type */
  130. 16, /* rightshift */
  131. 2, /* size (0 = byte, 1 = short, 2 = long) */
  132. 16, /* bitsize */
  133. FALSE, /* pc_relative */
  134. 0, /* bitpos */
  135. complain_overflow_bitfield, /* complain_on_overflow */
  136. mips_refhi_reloc, /* special_function */
  137. "REFHI", /* name */
  138. TRUE, /* partial_inplace */
  139. 0xffff, /* src_mask */
  140. 0xffff, /* dst_mask */
  141. FALSE), /* pcrel_offset */
  142. /* The low 16 bits of a symbol value. */
  143. HOWTO (MIPS_R_REFLO, /* type */
  144. 0, /* rightshift */
  145. 2, /* size (0 = byte, 1 = short, 2 = long) */
  146. 16, /* bitsize */
  147. FALSE, /* pc_relative */
  148. 0, /* bitpos */
  149. complain_overflow_dont, /* complain_on_overflow */
  150. mips_reflo_reloc, /* special_function */
  151. "REFLO", /* name */
  152. TRUE, /* partial_inplace */
  153. 0xffff, /* src_mask */
  154. 0xffff, /* dst_mask */
  155. FALSE), /* pcrel_offset */
  156. /* A reference to an offset from the gp register. Handled by the
  157. function mips_gprel_reloc. */
  158. HOWTO (MIPS_R_GPREL, /* type */
  159. 0, /* rightshift */
  160. 2, /* size (0 = byte, 1 = short, 2 = long) */
  161. 16, /* bitsize */
  162. FALSE, /* pc_relative */
  163. 0, /* bitpos */
  164. complain_overflow_signed, /* complain_on_overflow */
  165. mips_gprel_reloc, /* special_function */
  166. "GPREL", /* name */
  167. TRUE, /* partial_inplace */
  168. 0xffff, /* src_mask */
  169. 0xffff, /* dst_mask */
  170. FALSE), /* pcrel_offset */
  171. /* A reference to a literal using an offset from the gp register.
  172. Handled by the function mips_gprel_reloc. */
  173. HOWTO (MIPS_R_LITERAL, /* type */
  174. 0, /* rightshift */
  175. 2, /* size (0 = byte, 1 = short, 2 = long) */
  176. 16, /* bitsize */
  177. FALSE, /* pc_relative */
  178. 0, /* bitpos */
  179. complain_overflow_signed, /* complain_on_overflow */
  180. mips_gprel_reloc, /* special_function */
  181. "LITERAL", /* name */
  182. TRUE, /* partial_inplace */
  183. 0xffff, /* src_mask */
  184. 0xffff, /* dst_mask */
  185. FALSE), /* pcrel_offset */
  186. EMPTY_HOWTO (8),
  187. EMPTY_HOWTO (9),
  188. EMPTY_HOWTO (10),
  189. EMPTY_HOWTO (11),
  190. /* FIXME: This relocation is used (internally only) to represent branches
  191. when assembling. It should never appear in output files, and
  192. be removed. (It used to be used for embedded-PIC support.) */
  193. HOWTO (MIPS_R_PCREL16, /* type */
  194. 2, /* rightshift */
  195. 2, /* size (0 = byte, 1 = short, 2 = long) */
  196. 16, /* bitsize */
  197. TRUE, /* pc_relative */
  198. 0, /* bitpos */
  199. complain_overflow_signed, /* complain_on_overflow */
  200. mips_generic_reloc, /* special_function */
  201. "PCREL16", /* name */
  202. TRUE, /* partial_inplace */
  203. 0xffff, /* src_mask */
  204. 0xffff, /* dst_mask */
  205. TRUE), /* pcrel_offset */
  206. };
  207. #define MIPS_HOWTO_COUNT \
  208. (sizeof mips_howto_table / sizeof mips_howto_table[0])
  209. /* See whether the magic number matches. */
  210. static bfd_boolean
  211. mips_ecoff_bad_format_hook (bfd * abfd, void * filehdr)
  212. {
  213. struct internal_filehdr *internal_f = (struct internal_filehdr *) filehdr;
  214. switch (internal_f->f_magic)
  215. {
  216. case MIPS_MAGIC_1:
  217. /* I don't know what endianness this implies. */
  218. return TRUE;
  219. case MIPS_MAGIC_BIG:
  220. case MIPS_MAGIC_BIG2:
  221. case MIPS_MAGIC_BIG3:
  222. return bfd_big_endian (abfd);
  223. case MIPS_MAGIC_LITTLE:
  224. case MIPS_MAGIC_LITTLE2:
  225. case MIPS_MAGIC_LITTLE3:
  226. return bfd_little_endian (abfd);
  227. default:
  228. return FALSE;
  229. }
  230. }
  231. /* Reloc handling. MIPS ECOFF relocs are packed into 8 bytes in
  232. external form. They use a bit which indicates whether the symbol
  233. is external. */
  234. /* Swap a reloc in. */
  235. static void
  236. mips_ecoff_swap_reloc_in (bfd * abfd,
  237. void * ext_ptr,
  238. struct internal_reloc *intern)
  239. {
  240. const RELOC *ext = (RELOC *) ext_ptr;
  241. intern->r_vaddr = H_GET_32 (abfd, ext->r_vaddr);
  242. if (bfd_header_big_endian (abfd))
  243. {
  244. intern->r_symndx = (((int) ext->r_bits[0]
  245. << RELOC_BITS0_SYMNDX_SH_LEFT_BIG)
  246. | ((int) ext->r_bits[1]
  247. << RELOC_BITS1_SYMNDX_SH_LEFT_BIG)
  248. | ((int) ext->r_bits[2]
  249. << RELOC_BITS2_SYMNDX_SH_LEFT_BIG));
  250. intern->r_type = ((ext->r_bits[3] & RELOC_BITS3_TYPE_BIG)
  251. >> RELOC_BITS3_TYPE_SH_BIG);
  252. intern->r_extern = (ext->r_bits[3] & RELOC_BITS3_EXTERN_BIG) != 0;
  253. }
  254. else
  255. {
  256. intern->r_symndx = (((int) ext->r_bits[0]
  257. << RELOC_BITS0_SYMNDX_SH_LEFT_LITTLE)
  258. | ((int) ext->r_bits[1]
  259. << RELOC_BITS1_SYMNDX_SH_LEFT_LITTLE)
  260. | ((int) ext->r_bits[2]
  261. << RELOC_BITS2_SYMNDX_SH_LEFT_LITTLE));
  262. intern->r_type = (((ext->r_bits[3] & RELOC_BITS3_TYPE_LITTLE)
  263. >> RELOC_BITS3_TYPE_SH_LITTLE)
  264. | ((ext->r_bits[3] & RELOC_BITS3_TYPEHI_LITTLE)
  265. << RELOC_BITS3_TYPEHI_SH_LITTLE));
  266. intern->r_extern = (ext->r_bits[3] & RELOC_BITS3_EXTERN_LITTLE) != 0;
  267. }
  268. }
  269. /* Swap a reloc out. */
  270. static void
  271. mips_ecoff_swap_reloc_out (bfd * abfd,
  272. const struct internal_reloc * intern,
  273. void * dst)
  274. {
  275. RELOC *ext = (RELOC *) dst;
  276. long r_symndx;
  277. BFD_ASSERT (intern->r_extern
  278. || (intern->r_symndx >= 0 && intern->r_symndx <= 12));
  279. r_symndx = intern->r_symndx;
  280. H_PUT_32 (abfd, intern->r_vaddr, ext->r_vaddr);
  281. if (bfd_header_big_endian (abfd))
  282. {
  283. ext->r_bits[0] = r_symndx >> RELOC_BITS0_SYMNDX_SH_LEFT_BIG;
  284. ext->r_bits[1] = r_symndx >> RELOC_BITS1_SYMNDX_SH_LEFT_BIG;
  285. ext->r_bits[2] = r_symndx >> RELOC_BITS2_SYMNDX_SH_LEFT_BIG;
  286. ext->r_bits[3] = (((intern->r_type << RELOC_BITS3_TYPE_SH_BIG)
  287. & RELOC_BITS3_TYPE_BIG)
  288. | (intern->r_extern ? RELOC_BITS3_EXTERN_BIG : 0));
  289. }
  290. else
  291. {
  292. ext->r_bits[0] = r_symndx >> RELOC_BITS0_SYMNDX_SH_LEFT_LITTLE;
  293. ext->r_bits[1] = r_symndx >> RELOC_BITS1_SYMNDX_SH_LEFT_LITTLE;
  294. ext->r_bits[2] = r_symndx >> RELOC_BITS2_SYMNDX_SH_LEFT_LITTLE;
  295. ext->r_bits[3] = (((intern->r_type << RELOC_BITS3_TYPE_SH_LITTLE)
  296. & RELOC_BITS3_TYPE_LITTLE)
  297. | ((intern->r_type >> RELOC_BITS3_TYPEHI_SH_LITTLE
  298. & RELOC_BITS3_TYPEHI_LITTLE))
  299. | (intern->r_extern ? RELOC_BITS3_EXTERN_LITTLE : 0));
  300. }
  301. }
  302. /* Finish canonicalizing a reloc. Part of this is generic to all
  303. ECOFF targets, and that part is in ecoff.c. The rest is done in
  304. this backend routine. It must fill in the howto field. */
  305. static void
  306. mips_adjust_reloc_in (bfd *abfd,
  307. const struct internal_reloc *intern,
  308. arelent *rptr)
  309. {
  310. if (intern->r_type > MIPS_R_PCREL16)
  311. abort ();
  312. if (! intern->r_extern
  313. && (intern->r_type == MIPS_R_GPREL
  314. || intern->r_type == MIPS_R_LITERAL))
  315. rptr->addend += ecoff_data (abfd)->gp;
  316. /* If the type is MIPS_R_IGNORE, make sure this is a reference to
  317. the absolute section so that the reloc is ignored. */
  318. if (intern->r_type == MIPS_R_IGNORE)
  319. rptr->sym_ptr_ptr = bfd_abs_section_ptr->symbol_ptr_ptr;
  320. rptr->howto = &mips_howto_table[intern->r_type];
  321. }
  322. /* Make any adjustments needed to a reloc before writing it out. None
  323. are needed for MIPS. */
  324. static void
  325. mips_adjust_reloc_out (bfd *abfd ATTRIBUTE_UNUSED,
  326. const arelent *rel ATTRIBUTE_UNUSED,
  327. struct internal_reloc *intern ATTRIBUTE_UNUSED)
  328. {
  329. }
  330. /* ECOFF relocs are either against external symbols, or against
  331. sections. If we are producing relocatable output, and the reloc
  332. is against an external symbol, and nothing has given us any
  333. additional addend, the resulting reloc will also be against the
  334. same symbol. In such a case, we don't want to change anything
  335. about the way the reloc is handled, since it will all be done at
  336. final link time. Rather than put special case code into
  337. bfd_perform_relocation, all the reloc types use this howto
  338. function. It just short circuits the reloc if producing
  339. relocatable output against an external symbol. */
  340. static bfd_reloc_status_type
  341. mips_generic_reloc (bfd *abfd ATTRIBUTE_UNUSED,
  342. arelent *reloc_entry,
  343. asymbol *symbol,
  344. void * data ATTRIBUTE_UNUSED,
  345. asection *input_section,
  346. bfd *output_bfd,
  347. char **error_message ATTRIBUTE_UNUSED)
  348. {
  349. if (output_bfd != (bfd *) NULL
  350. && (symbol->flags & BSF_SECTION_SYM) == 0
  351. && reloc_entry->addend == 0)
  352. {
  353. reloc_entry->address += input_section->output_offset;
  354. return bfd_reloc_ok;
  355. }
  356. return bfd_reloc_continue;
  357. }
  358. /* Do a REFHI relocation. This has to be done in combination with a
  359. REFLO reloc, because there is a carry from the REFLO to the REFHI.
  360. Here we just save the information we need; we do the actual
  361. relocation when we see the REFLO. MIPS ECOFF requires that the
  362. REFLO immediately follow the REFHI. As a GNU extension, we permit
  363. an arbitrary number of HI relocs to be associated with a single LO
  364. reloc. This extension permits gcc to output the HI and LO relocs
  365. itself. */
  366. struct mips_hi
  367. {
  368. struct mips_hi *next;
  369. bfd_byte *addr;
  370. bfd_vma addend;
  371. };
  372. /* FIXME: This should not be a static variable. */
  373. static struct mips_hi *mips_refhi_list;
  374. static bfd_reloc_status_type
  375. mips_refhi_reloc (bfd *abfd ATTRIBUTE_UNUSED,
  376. arelent *reloc_entry,
  377. asymbol *symbol,
  378. void * data,
  379. asection *input_section,
  380. bfd *output_bfd,
  381. char **error_message ATTRIBUTE_UNUSED)
  382. {
  383. bfd_reloc_status_type ret;
  384. bfd_vma relocation;
  385. struct mips_hi *n;
  386. /* If we're relocating, and this an external symbol, we don't want
  387. to change anything. */
  388. if (output_bfd != (bfd *) NULL
  389. && (symbol->flags & BSF_SECTION_SYM) == 0
  390. && reloc_entry->addend == 0)
  391. {
  392. reloc_entry->address += input_section->output_offset;
  393. return bfd_reloc_ok;
  394. }
  395. ret = bfd_reloc_ok;
  396. if (bfd_is_und_section (symbol->section)
  397. && output_bfd == (bfd *) NULL)
  398. ret = bfd_reloc_undefined;
  399. if (bfd_is_com_section (symbol->section))
  400. relocation = 0;
  401. else
  402. relocation = symbol->value;
  403. relocation += symbol->section->output_section->vma;
  404. relocation += symbol->section->output_offset;
  405. relocation += reloc_entry->addend;
  406. if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
  407. return bfd_reloc_outofrange;
  408. /* Save the information, and let REFLO do the actual relocation. */
  409. n = (struct mips_hi *) bfd_malloc ((bfd_size_type) sizeof *n);
  410. if (n == NULL)
  411. return bfd_reloc_outofrange;
  412. n->addr = (bfd_byte *) data + reloc_entry->address;
  413. n->addend = relocation;
  414. n->next = mips_refhi_list;
  415. mips_refhi_list = n;
  416. if (output_bfd != (bfd *) NULL)
  417. reloc_entry->address += input_section->output_offset;
  418. return ret;
  419. }
  420. /* Do a REFLO relocation. This is a straightforward 16 bit inplace
  421. relocation; this function exists in order to do the REFHI
  422. relocation described above. */
  423. static bfd_reloc_status_type
  424. mips_reflo_reloc (bfd *abfd ATTRIBUTE_UNUSED,
  425. arelent *reloc_entry,
  426. asymbol *symbol,
  427. void * data,
  428. asection *input_section,
  429. bfd *output_bfd,
  430. char **error_message ATTRIBUTE_UNUSED)
  431. {
  432. if (mips_refhi_list != NULL)
  433. {
  434. struct mips_hi *l;
  435. l = mips_refhi_list;
  436. while (l != NULL)
  437. {
  438. unsigned long insn;
  439. unsigned long val;
  440. unsigned long vallo;
  441. struct mips_hi *next;
  442. /* Do the REFHI relocation. Note that we actually don't
  443. need to know anything about the REFLO itself, except
  444. where to find the low 16 bits of the addend needed by the
  445. REFHI. */
  446. insn = bfd_get_32 (abfd, l->addr);
  447. vallo = (bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address)
  448. & 0xffff);
  449. val = ((insn & 0xffff) << 16) + vallo;
  450. val += l->addend;
  451. /* The low order 16 bits are always treated as a signed
  452. value. Therefore, a negative value in the low order bits
  453. requires an adjustment in the high order bits. We need
  454. to make this adjustment in two ways: once for the bits we
  455. took from the data, and once for the bits we are putting
  456. back in to the data. */
  457. if ((vallo & 0x8000) != 0)
  458. val -= 0x10000;
  459. if ((val & 0x8000) != 0)
  460. val += 0x10000;
  461. insn = (insn &~ (unsigned) 0xffff) | ((val >> 16) & 0xffff);
  462. bfd_put_32 (abfd, (bfd_vma) insn, l->addr);
  463. next = l->next;
  464. free (l);
  465. l = next;
  466. }
  467. mips_refhi_list = NULL;
  468. }
  469. /* Now do the REFLO reloc in the usual way. */
  470. return mips_generic_reloc (abfd, reloc_entry, symbol, data,
  471. input_section, output_bfd, error_message);
  472. }
  473. /* Do a GPREL relocation. This is a 16 bit value which must become
  474. the offset from the gp register. */
  475. static bfd_reloc_status_type
  476. mips_gprel_reloc (bfd *abfd ATTRIBUTE_UNUSED,
  477. arelent *reloc_entry,
  478. asymbol *symbol,
  479. void * data,
  480. asection *input_section,
  481. bfd *output_bfd,
  482. char **error_message ATTRIBUTE_UNUSED)
  483. {
  484. bfd_boolean relocatable;
  485. bfd_vma gp;
  486. bfd_vma relocation;
  487. unsigned long val;
  488. unsigned long insn;
  489. /* If we're relocating, and this is an external symbol with no
  490. addend, we don't want to change anything. We will only have an
  491. addend if this is a newly created reloc, not read from an ECOFF
  492. file. */
  493. if (output_bfd != (bfd *) NULL
  494. && (symbol->flags & BSF_SECTION_SYM) == 0
  495. && reloc_entry->addend == 0)
  496. {
  497. reloc_entry->address += input_section->output_offset;
  498. return bfd_reloc_ok;
  499. }
  500. if (output_bfd != (bfd *) NULL)
  501. relocatable = TRUE;
  502. else
  503. {
  504. relocatable = FALSE;
  505. output_bfd = symbol->section->output_section->owner;
  506. }
  507. if (bfd_is_und_section (symbol->section) && ! relocatable)
  508. return bfd_reloc_undefined;
  509. /* We have to figure out the gp value, so that we can adjust the
  510. symbol value correctly. We look up the symbol _gp in the output
  511. BFD. If we can't find it, we're stuck. We cache it in the ECOFF
  512. target data. We don't need to adjust the symbol value for an
  513. external symbol if we are producing relocatable output. */
  514. gp = _bfd_get_gp_value (output_bfd);
  515. if (gp == 0
  516. && (! relocatable
  517. || (symbol->flags & BSF_SECTION_SYM) != 0))
  518. {
  519. if (relocatable)
  520. {
  521. /* Make up a value. */
  522. gp = symbol->section->output_section->vma + 0x4000;
  523. _bfd_set_gp_value (output_bfd, gp);
  524. }
  525. else
  526. {
  527. unsigned int count;
  528. asymbol **sym;
  529. unsigned int i;
  530. count = bfd_get_symcount (output_bfd);
  531. sym = bfd_get_outsymbols (output_bfd);
  532. if (sym == (asymbol **) NULL)
  533. i = count;
  534. else
  535. {
  536. for (i = 0; i < count; i++, sym++)
  537. {
  538. register const char *name;
  539. name = bfd_asymbol_name (*sym);
  540. if (*name == '_' && strcmp (name, "_gp") == 0)
  541. {
  542. gp = bfd_asymbol_value (*sym);
  543. _bfd_set_gp_value (output_bfd, gp);
  544. break;
  545. }
  546. }
  547. }
  548. if (i >= count)
  549. {
  550. /* Only get the error once. */
  551. gp = 4;
  552. _bfd_set_gp_value (output_bfd, gp);
  553. *error_message =
  554. (char *) _("GP relative relocation when _gp not defined");
  555. return bfd_reloc_dangerous;
  556. }
  557. }
  558. }
  559. if (bfd_is_com_section (symbol->section))
  560. relocation = 0;
  561. else
  562. relocation = symbol->value;
  563. relocation += symbol->section->output_section->vma;
  564. relocation += symbol->section->output_offset;
  565. if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
  566. return bfd_reloc_outofrange;
  567. insn = bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address);
  568. /* Set val to the offset into the section or symbol. */
  569. val = ((insn & 0xffff) + reloc_entry->addend) & 0xffff;
  570. if (val & 0x8000)
  571. val -= 0x10000;
  572. /* Adjust val for the final section location and GP value. If we
  573. are producing relocatable output, we don't want to do this for
  574. an external symbol. */
  575. if (! relocatable
  576. || (symbol->flags & BSF_SECTION_SYM) != 0)
  577. val += relocation - gp;
  578. insn = (insn &~ (unsigned) 0xffff) | (val & 0xffff);
  579. bfd_put_32 (abfd, (bfd_vma) insn, (bfd_byte *) data + reloc_entry->address);
  580. if (relocatable)
  581. reloc_entry->address += input_section->output_offset;
  582. /* Make sure it fit in 16 bits. */
  583. if ((long) val >= 0x8000 || (long) val < -0x8000)
  584. return bfd_reloc_overflow;
  585. return bfd_reloc_ok;
  586. }
  587. /* Get the howto structure for a generic reloc type. */
  588. static reloc_howto_type *
  589. mips_bfd_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
  590. bfd_reloc_code_real_type code)
  591. {
  592. int mips_type;
  593. switch (code)
  594. {
  595. case BFD_RELOC_16:
  596. mips_type = MIPS_R_REFHALF;
  597. break;
  598. case BFD_RELOC_32:
  599. case BFD_RELOC_CTOR:
  600. mips_type = MIPS_R_REFWORD;
  601. break;
  602. case BFD_RELOC_MIPS_JMP:
  603. mips_type = MIPS_R_JMPADDR;
  604. break;
  605. case BFD_RELOC_HI16_S:
  606. mips_type = MIPS_R_REFHI;
  607. break;
  608. case BFD_RELOC_LO16:
  609. mips_type = MIPS_R_REFLO;
  610. break;
  611. case BFD_RELOC_GPREL16:
  612. mips_type = MIPS_R_GPREL;
  613. break;
  614. case BFD_RELOC_MIPS_LITERAL:
  615. mips_type = MIPS_R_LITERAL;
  616. break;
  617. case BFD_RELOC_16_PCREL_S2:
  618. mips_type = MIPS_R_PCREL16;
  619. break;
  620. default:
  621. return (reloc_howto_type *) NULL;
  622. }
  623. return &mips_howto_table[mips_type];
  624. }
  625. static reloc_howto_type *
  626. mips_bfd_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
  627. const char *r_name)
  628. {
  629. unsigned int i;
  630. for (i = 0;
  631. i < sizeof (mips_howto_table) / sizeof (mips_howto_table[0]);
  632. i++)
  633. if (mips_howto_table[i].name != NULL
  634. && strcasecmp (mips_howto_table[i].name, r_name) == 0)
  635. return &mips_howto_table[i];
  636. return NULL;
  637. }
  638. /* A helper routine for mips_relocate_section which handles the REFHI
  639. relocations. The REFHI relocation must be followed by a REFLO
  640. relocation, and the addend used is formed from the addends of both
  641. instructions. */
  642. static void
  643. mips_relocate_hi (struct internal_reloc *refhi,
  644. struct internal_reloc *reflo,
  645. bfd *input_bfd,
  646. asection *input_section,
  647. bfd_byte *contents,
  648. bfd_vma relocation)
  649. {
  650. unsigned long insn;
  651. unsigned long val;
  652. unsigned long vallo;
  653. if (refhi == NULL)
  654. return;
  655. insn = bfd_get_32 (input_bfd,
  656. contents + refhi->r_vaddr - input_section->vma);
  657. if (reflo == NULL)
  658. vallo = 0;
  659. else
  660. vallo = (bfd_get_32 (input_bfd,
  661. contents + reflo->r_vaddr - input_section->vma)
  662. & 0xffff);
  663. val = ((insn & 0xffff) << 16) + vallo;
  664. val += relocation;
  665. /* The low order 16 bits are always treated as a signed value.
  666. Therefore, a negative value in the low order bits requires an
  667. adjustment in the high order bits. We need to make this
  668. adjustment in two ways: once for the bits we took from the data,
  669. and once for the bits we are putting back in to the data. */
  670. if ((vallo & 0x8000) != 0)
  671. val -= 0x10000;
  672. if ((val & 0x8000) != 0)
  673. val += 0x10000;
  674. insn = (insn &~ (unsigned) 0xffff) | ((val >> 16) & 0xffff);
  675. bfd_put_32 (input_bfd, (bfd_vma) insn,
  676. contents + refhi->r_vaddr - input_section->vma);
  677. }
  678. /* Relocate a section while linking a MIPS ECOFF file. */
  679. static bfd_boolean
  680. mips_relocate_section (bfd *output_bfd,
  681. struct bfd_link_info *info,
  682. bfd *input_bfd,
  683. asection *input_section,
  684. bfd_byte *contents,
  685. void * external_relocs)
  686. {
  687. asection **symndx_to_section;
  688. struct ecoff_link_hash_entry **sym_hashes;
  689. bfd_vma gp;
  690. bfd_boolean gp_undefined;
  691. struct external_reloc *ext_rel;
  692. struct external_reloc *ext_rel_end;
  693. unsigned int i;
  694. bfd_boolean got_lo;
  695. struct internal_reloc lo_int_rel;
  696. bfd_size_type amt;
  697. BFD_ASSERT (input_bfd->xvec->byteorder
  698. == output_bfd->xvec->byteorder);
  699. /* We keep a table mapping the symndx found in an internal reloc to
  700. the appropriate section. This is faster than looking up the
  701. section by name each time. */
  702. symndx_to_section = ecoff_data (input_bfd)->symndx_to_section;
  703. if (symndx_to_section == (asection **) NULL)
  704. {
  705. amt = NUM_RELOC_SECTIONS * sizeof (asection *);
  706. symndx_to_section = (asection **) bfd_alloc (input_bfd, amt);
  707. if (!symndx_to_section)
  708. return FALSE;
  709. symndx_to_section[RELOC_SECTION_NONE] = NULL;
  710. symndx_to_section[RELOC_SECTION_TEXT] =
  711. bfd_get_section_by_name (input_bfd, ".text");
  712. symndx_to_section[RELOC_SECTION_RDATA] =
  713. bfd_get_section_by_name (input_bfd, ".rdata");
  714. symndx_to_section[RELOC_SECTION_DATA] =
  715. bfd_get_section_by_name (input_bfd, ".data");
  716. symndx_to_section[RELOC_SECTION_SDATA] =
  717. bfd_get_section_by_name (input_bfd, ".sdata");
  718. symndx_to_section[RELOC_SECTION_SBSS] =
  719. bfd_get_section_by_name (input_bfd, ".sbss");
  720. symndx_to_section[RELOC_SECTION_BSS] =
  721. bfd_get_section_by_name (input_bfd, ".bss");
  722. symndx_to_section[RELOC_SECTION_INIT] =
  723. bfd_get_section_by_name (input_bfd, ".init");
  724. symndx_to_section[RELOC_SECTION_LIT8] =
  725. bfd_get_section_by_name (input_bfd, ".lit8");
  726. symndx_to_section[RELOC_SECTION_LIT4] =
  727. bfd_get_section_by_name (input_bfd, ".lit4");
  728. symndx_to_section[RELOC_SECTION_XDATA] = NULL;
  729. symndx_to_section[RELOC_SECTION_PDATA] = NULL;
  730. symndx_to_section[RELOC_SECTION_FINI] =
  731. bfd_get_section_by_name (input_bfd, ".fini");
  732. symndx_to_section[RELOC_SECTION_LITA] = NULL;
  733. symndx_to_section[RELOC_SECTION_ABS] = NULL;
  734. ecoff_data (input_bfd)->symndx_to_section = symndx_to_section;
  735. }
  736. sym_hashes = ecoff_data (input_bfd)->sym_hashes;
  737. gp = _bfd_get_gp_value (output_bfd);
  738. if (gp == 0)
  739. gp_undefined = TRUE;
  740. else
  741. gp_undefined = FALSE;
  742. got_lo = FALSE;
  743. ext_rel = (struct external_reloc *) external_relocs;
  744. ext_rel_end = ext_rel + input_section->reloc_count;
  745. for (i = 0; ext_rel < ext_rel_end; ext_rel++, i++)
  746. {
  747. struct internal_reloc int_rel;
  748. bfd_boolean use_lo = FALSE;
  749. bfd_vma addend;
  750. reloc_howto_type *howto;
  751. struct ecoff_link_hash_entry *h = NULL;
  752. asection *s = NULL;
  753. bfd_vma relocation;
  754. bfd_reloc_status_type r;
  755. if (! got_lo)
  756. mips_ecoff_swap_reloc_in (input_bfd, ext_rel, &int_rel);
  757. else
  758. {
  759. int_rel = lo_int_rel;
  760. got_lo = FALSE;
  761. }
  762. BFD_ASSERT (int_rel.r_type
  763. < sizeof mips_howto_table / sizeof mips_howto_table[0]);
  764. /* The REFHI reloc requires special handling. It must be followed
  765. by a REFLO reloc, and the addend is formed from both relocs. */
  766. if (int_rel.r_type == MIPS_R_REFHI)
  767. {
  768. struct external_reloc *lo_ext_rel;
  769. /* As a GNU extension, permit an arbitrary number of REFHI
  770. relocs before the REFLO reloc. This permits gcc to emit
  771. the HI and LO relocs itself. */
  772. for (lo_ext_rel = ext_rel + 1;
  773. lo_ext_rel < ext_rel_end;
  774. lo_ext_rel++)
  775. {
  776. mips_ecoff_swap_reloc_in (input_bfd, lo_ext_rel,
  777. &lo_int_rel);
  778. if (lo_int_rel.r_type != int_rel.r_type)
  779. break;
  780. }
  781. if (lo_ext_rel < ext_rel_end
  782. && lo_int_rel.r_type == MIPS_R_REFLO
  783. && int_rel.r_extern == lo_int_rel.r_extern
  784. && int_rel.r_symndx == lo_int_rel.r_symndx)
  785. {
  786. use_lo = TRUE;
  787. if (lo_ext_rel == ext_rel + 1)
  788. got_lo = TRUE;
  789. }
  790. }
  791. howto = &mips_howto_table[int_rel.r_type];
  792. if (int_rel.r_extern)
  793. {
  794. h = sym_hashes[int_rel.r_symndx];
  795. /* If h is NULL, that means that there is a reloc against an
  796. external symbol which we thought was just a debugging
  797. symbol. This should not happen. */
  798. if (h == (struct ecoff_link_hash_entry *) NULL)
  799. abort ();
  800. }
  801. else
  802. {
  803. if (int_rel.r_symndx < 0 || int_rel.r_symndx >= NUM_RELOC_SECTIONS)
  804. s = NULL;
  805. else
  806. s = symndx_to_section[int_rel.r_symndx];
  807. if (s == (asection *) NULL)
  808. abort ();
  809. }
  810. /* The GPREL reloc uses an addend: the difference in the GP
  811. values. */
  812. if (int_rel.r_type != MIPS_R_GPREL
  813. && int_rel.r_type != MIPS_R_LITERAL)
  814. addend = 0;
  815. else
  816. {
  817. if (gp_undefined)
  818. {
  819. if (! ((*info->callbacks->reloc_dangerous)
  820. (info, _("GP relative relocation used when GP not defined"),
  821. input_bfd, input_section,
  822. int_rel.r_vaddr - input_section->vma)))
  823. return FALSE;
  824. /* Only give the error once per link. */
  825. gp = 4;
  826. _bfd_set_gp_value (output_bfd, gp);
  827. gp_undefined = FALSE;
  828. }
  829. if (! int_rel.r_extern)
  830. {
  831. /* This is a relocation against a section. The current
  832. addend in the instruction is the difference between
  833. INPUT_SECTION->vma and the GP value of INPUT_BFD. We
  834. must change this to be the difference between the
  835. final definition (which will end up in RELOCATION)
  836. and the GP value of OUTPUT_BFD (which is in GP). */
  837. addend = ecoff_data (input_bfd)->gp - gp;
  838. }
  839. else if (! bfd_link_relocatable (info)
  840. || h->root.type == bfd_link_hash_defined
  841. || h->root.type == bfd_link_hash_defweak)
  842. {
  843. /* This is a relocation against a defined symbol. The
  844. current addend in the instruction is simply the
  845. desired offset into the symbol (normally zero). We
  846. are going to change this into a relocation against a
  847. defined symbol, so we want the instruction to hold
  848. the difference between the final definition of the
  849. symbol (which will end up in RELOCATION) and the GP
  850. value of OUTPUT_BFD (which is in GP). */
  851. addend = - gp;
  852. }
  853. else
  854. {
  855. /* This is a relocation against an undefined or common
  856. symbol. The current addend in the instruction is
  857. simply the desired offset into the symbol (normally
  858. zero). We are generating relocatable output, and we
  859. aren't going to define this symbol, so we just leave
  860. the instruction alone. */
  861. addend = 0;
  862. }
  863. }
  864. if (bfd_link_relocatable (info))
  865. {
  866. /* We are generating relocatable output, and must convert
  867. the existing reloc. */
  868. if (int_rel.r_extern)
  869. {
  870. if ((h->root.type == bfd_link_hash_defined
  871. || h->root.type == bfd_link_hash_defweak)
  872. && ! bfd_is_abs_section (h->root.u.def.section))
  873. {
  874. const char *name;
  875. /* This symbol is defined in the output. Convert
  876. the reloc from being against the symbol to being
  877. against the section. */
  878. /* Clear the r_extern bit. */
  879. int_rel.r_extern = 0;
  880. /* Compute a new r_symndx value. */
  881. s = h->root.u.def.section;
  882. name = bfd_get_section_name (output_bfd,
  883. s->output_section);
  884. int_rel.r_symndx = -1;
  885. switch (name[1])
  886. {
  887. case 'b':
  888. if (strcmp (name, ".bss") == 0)
  889. int_rel.r_symndx = RELOC_SECTION_BSS;
  890. break;
  891. case 'd':
  892. if (strcmp (name, ".data") == 0)
  893. int_rel.r_symndx = RELOC_SECTION_DATA;
  894. break;
  895. case 'f':
  896. if (strcmp (name, ".fini") == 0)
  897. int_rel.r_symndx = RELOC_SECTION_FINI;
  898. break;
  899. case 'i':
  900. if (strcmp (name, ".init") == 0)
  901. int_rel.r_symndx = RELOC_SECTION_INIT;
  902. break;
  903. case 'l':
  904. if (strcmp (name, ".lit8") == 0)
  905. int_rel.r_symndx = RELOC_SECTION_LIT8;
  906. else if (strcmp (name, ".lit4") == 0)
  907. int_rel.r_symndx = RELOC_SECTION_LIT4;
  908. break;
  909. case 'r':
  910. if (strcmp (name, ".rdata") == 0)
  911. int_rel.r_symndx = RELOC_SECTION_RDATA;
  912. break;
  913. case 's':
  914. if (strcmp (name, ".sdata") == 0)
  915. int_rel.r_symndx = RELOC_SECTION_SDATA;
  916. else if (strcmp (name, ".sbss") == 0)
  917. int_rel.r_symndx = RELOC_SECTION_SBSS;
  918. break;
  919. case 't':
  920. if (strcmp (name, ".text") == 0)
  921. int_rel.r_symndx = RELOC_SECTION_TEXT;
  922. break;
  923. }
  924. if (int_rel.r_symndx == -1)
  925. abort ();
  926. /* Add the section VMA and the symbol value. */
  927. relocation = (h->root.u.def.value
  928. + s->output_section->vma
  929. + s->output_offset);
  930. /* For a PC relative relocation, the object file
  931. currently holds just the addend. We must adjust
  932. by the address to get the right value. */
  933. if (howto->pc_relative)
  934. relocation -= int_rel.r_vaddr - input_section->vma;
  935. h = NULL;
  936. }
  937. else
  938. {
  939. /* Change the symndx value to the right one for the
  940. output BFD. */
  941. int_rel.r_symndx = h->indx;
  942. if (int_rel.r_symndx == -1)
  943. {
  944. /* This symbol is not being written out. */
  945. if (! ((*info->callbacks->unattached_reloc)
  946. (info, h->root.root.string, input_bfd,
  947. input_section,
  948. int_rel.r_vaddr - input_section->vma)))
  949. return FALSE;
  950. int_rel.r_symndx = 0;
  951. }
  952. relocation = 0;
  953. }
  954. }
  955. else
  956. {
  957. /* This is a relocation against a section. Adjust the
  958. value by the amount the section moved. */
  959. relocation = (s->output_section->vma
  960. + s->output_offset
  961. - s->vma);
  962. }
  963. relocation += addend;
  964. addend = 0;
  965. /* Adjust a PC relative relocation by removing the reference
  966. to the original address in the section and including the
  967. reference to the new address. */
  968. if (howto->pc_relative)
  969. relocation -= (input_section->output_section->vma
  970. + input_section->output_offset
  971. - input_section->vma);
  972. /* Adjust the contents. */
  973. if (relocation == 0)
  974. r = bfd_reloc_ok;
  975. else
  976. {
  977. if (int_rel.r_type != MIPS_R_REFHI)
  978. r = _bfd_relocate_contents (howto, input_bfd, relocation,
  979. (contents
  980. + int_rel.r_vaddr
  981. - input_section->vma));
  982. else
  983. {
  984. mips_relocate_hi (&int_rel,
  985. use_lo ? &lo_int_rel : NULL,
  986. input_bfd, input_section, contents,
  987. relocation);
  988. r = bfd_reloc_ok;
  989. }
  990. }
  991. /* Adjust the reloc address. */
  992. int_rel.r_vaddr += (input_section->output_section->vma
  993. + input_section->output_offset
  994. - input_section->vma);
  995. /* Save the changed reloc information. */
  996. mips_ecoff_swap_reloc_out (input_bfd, &int_rel, ext_rel);
  997. }
  998. else
  999. {
  1000. /* We are producing a final executable. */
  1001. if (int_rel.r_extern)
  1002. {
  1003. /* This is a reloc against a symbol. */
  1004. if (h->root.type == bfd_link_hash_defined
  1005. || h->root.type == bfd_link_hash_defweak)
  1006. {
  1007. asection *hsec;
  1008. hsec = h->root.u.def.section;
  1009. relocation = (h->root.u.def.value
  1010. + hsec->output_section->vma
  1011. + hsec->output_offset);
  1012. }
  1013. else
  1014. {
  1015. if (! ((*info->callbacks->undefined_symbol)
  1016. (info, h->root.root.string, input_bfd,
  1017. input_section,
  1018. int_rel.r_vaddr - input_section->vma, TRUE)))
  1019. return FALSE;
  1020. relocation = 0;
  1021. }
  1022. }
  1023. else
  1024. {
  1025. /* This is a reloc against a section. */
  1026. relocation = (s->output_section->vma
  1027. + s->output_offset
  1028. - s->vma);
  1029. /* A PC relative reloc is already correct in the object
  1030. file. Make it look like a pcrel_offset relocation by
  1031. adding in the start address. */
  1032. if (howto->pc_relative)
  1033. relocation += int_rel.r_vaddr;
  1034. }
  1035. if (int_rel.r_type != MIPS_R_REFHI)
  1036. r = _bfd_final_link_relocate (howto,
  1037. input_bfd,
  1038. input_section,
  1039. contents,
  1040. (int_rel.r_vaddr
  1041. - input_section->vma),
  1042. relocation,
  1043. addend);
  1044. else
  1045. {
  1046. mips_relocate_hi (&int_rel,
  1047. use_lo ? &lo_int_rel : NULL,
  1048. input_bfd, input_section, contents,
  1049. relocation);
  1050. r = bfd_reloc_ok;
  1051. }
  1052. }
  1053. /* MIPS_R_JMPADDR requires peculiar overflow detection. The
  1054. instruction provides a 28 bit address (the two lower bits are
  1055. implicit zeroes) which is combined with the upper four bits
  1056. of the instruction address. */
  1057. if (r == bfd_reloc_ok
  1058. && int_rel.r_type == MIPS_R_JMPADDR
  1059. && (((relocation
  1060. + addend
  1061. + (int_rel.r_extern ? 0 : s->vma))
  1062. & 0xf0000000)
  1063. != ((input_section->output_section->vma
  1064. + input_section->output_offset
  1065. + (int_rel.r_vaddr - input_section->vma))
  1066. & 0xf0000000)))
  1067. r = bfd_reloc_overflow;
  1068. if (r != bfd_reloc_ok)
  1069. {
  1070. switch (r)
  1071. {
  1072. default:
  1073. case bfd_reloc_outofrange:
  1074. abort ();
  1075. case bfd_reloc_overflow:
  1076. {
  1077. const char *name;
  1078. if (int_rel.r_extern)
  1079. name = NULL;
  1080. else
  1081. name = bfd_section_name (input_bfd, s);
  1082. if (! ((*info->callbacks->reloc_overflow)
  1083. (info, (h ? &h->root : NULL), name, howto->name,
  1084. (bfd_vma) 0, input_bfd, input_section,
  1085. int_rel.r_vaddr - input_section->vma)))
  1086. return FALSE;
  1087. }
  1088. break;
  1089. }
  1090. }
  1091. }
  1092. return TRUE;
  1093. }
  1094. /* This is the ECOFF backend structure. The backend field of the
  1095. target vector points to this. */
  1096. static const struct ecoff_backend_data mips_ecoff_backend_data =
  1097. {
  1098. /* COFF backend structure. */
  1099. {
  1100. (void (*) (bfd *,void *,int,int,int,int,void *)) bfd_void, /* aux_in */
  1101. (void (*) (bfd *,void *,void *)) bfd_void, /* sym_in */
  1102. (void (*) (bfd *,void *,void *)) bfd_void, /* lineno_in */
  1103. (unsigned (*) (bfd *,void *,int,int,int,int,void *)) bfd_void,/*aux_out*/
  1104. (unsigned (*) (bfd *,void *,void *)) bfd_void, /* sym_out */
  1105. (unsigned (*) (bfd *,void *,void *)) bfd_void, /* lineno_out */
  1106. (unsigned (*) (bfd *,void *,void *)) bfd_void, /* reloc_out */
  1107. mips_ecoff_swap_filehdr_out, mips_ecoff_swap_aouthdr_out,
  1108. mips_ecoff_swap_scnhdr_out,
  1109. FILHSZ, AOUTSZ, SCNHSZ, 0, 0, 0, 0, FILNMLEN, TRUE,
  1110. ECOFF_NO_LONG_SECTION_NAMES, 4, FALSE, 2, 32768,
  1111. mips_ecoff_swap_filehdr_in, mips_ecoff_swap_aouthdr_in,
  1112. mips_ecoff_swap_scnhdr_in, NULL,
  1113. mips_ecoff_bad_format_hook, _bfd_ecoff_set_arch_mach_hook,
  1114. _bfd_ecoff_mkobject_hook, _bfd_ecoff_styp_to_sec_flags,
  1115. _bfd_ecoff_set_alignment_hook, _bfd_ecoff_slurp_symbol_table,
  1116. NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
  1117. NULL, NULL, NULL
  1118. },
  1119. /* Supported architecture. */
  1120. bfd_arch_mips,
  1121. /* Initial portion of armap string. */
  1122. "__________",
  1123. /* The page boundary used to align sections in a demand-paged
  1124. executable file. E.g., 0x1000. */
  1125. 0x1000,
  1126. /* TRUE if the .rdata section is part of the text segment, as on the
  1127. Alpha. FALSE if .rdata is part of the data segment, as on the
  1128. MIPS. */
  1129. FALSE,
  1130. /* Bitsize of constructor entries. */
  1131. 32,
  1132. /* Reloc to use for constructor entries. */
  1133. &mips_howto_table[MIPS_R_REFWORD],
  1134. {
  1135. /* Symbol table magic number. */
  1136. magicSym,
  1137. /* Alignment of debugging information. E.g., 4. */
  1138. 4,
  1139. /* Sizes of external symbolic information. */
  1140. sizeof (struct hdr_ext),
  1141. sizeof (struct dnr_ext),
  1142. sizeof (struct pdr_ext),
  1143. sizeof (struct sym_ext),
  1144. sizeof (struct opt_ext),
  1145. sizeof (struct fdr_ext),
  1146. sizeof (struct rfd_ext),
  1147. sizeof (struct ext_ext),
  1148. /* Functions to swap in external symbolic data. */
  1149. ecoff_swap_hdr_in,
  1150. ecoff_swap_dnr_in,
  1151. ecoff_swap_pdr_in,
  1152. ecoff_swap_sym_in,
  1153. ecoff_swap_opt_in,
  1154. ecoff_swap_fdr_in,
  1155. ecoff_swap_rfd_in,
  1156. ecoff_swap_ext_in,
  1157. _bfd_ecoff_swap_tir_in,
  1158. _bfd_ecoff_swap_rndx_in,
  1159. /* Functions to swap out external symbolic data. */
  1160. ecoff_swap_hdr_out,
  1161. ecoff_swap_dnr_out,
  1162. ecoff_swap_pdr_out,
  1163. ecoff_swap_sym_out,
  1164. ecoff_swap_opt_out,
  1165. ecoff_swap_fdr_out,
  1166. ecoff_swap_rfd_out,
  1167. ecoff_swap_ext_out,
  1168. _bfd_ecoff_swap_tir_out,
  1169. _bfd_ecoff_swap_rndx_out,
  1170. /* Function to read in symbolic data. */
  1171. _bfd_ecoff_slurp_symbolic_info
  1172. },
  1173. /* External reloc size. */
  1174. RELSZ,
  1175. /* Reloc swapping functions. */
  1176. mips_ecoff_swap_reloc_in,
  1177. mips_ecoff_swap_reloc_out,
  1178. /* Backend reloc tweaking. */
  1179. mips_adjust_reloc_in,
  1180. mips_adjust_reloc_out,
  1181. /* Relocate section contents while linking. */
  1182. mips_relocate_section,
  1183. /* Do final adjustments to filehdr and aouthdr. */
  1184. NULL,
  1185. /* Read an element from an archive at a given file position. */
  1186. _bfd_get_elt_at_filepos
  1187. };
  1188. /* Looking up a reloc type is MIPS specific. */
  1189. #define _bfd_ecoff_bfd_reloc_type_lookup mips_bfd_reloc_type_lookup
  1190. #define _bfd_ecoff_bfd_reloc_name_lookup mips_bfd_reloc_name_lookup
  1191. /* Getting relocated section contents is generic. */
  1192. #define _bfd_ecoff_bfd_get_relocated_section_contents \
  1193. bfd_generic_get_relocated_section_contents
  1194. /* Handling file windows is generic. */
  1195. #define _bfd_ecoff_get_section_contents_in_window \
  1196. _bfd_generic_get_section_contents_in_window
  1197. /* Relaxing sections is MIPS specific. */
  1198. #define _bfd_ecoff_bfd_relax_section bfd_generic_relax_section
  1199. /* GC of sections is not done. */
  1200. #define _bfd_ecoff_bfd_gc_sections bfd_generic_gc_sections
  1201. /* Input section flags is not implemented. */
  1202. #define _bfd_ecoff_bfd_lookup_section_flags bfd_generic_lookup_section_flags
  1203. /* Merging of sections is not done. */
  1204. #define _bfd_ecoff_bfd_merge_sections bfd_generic_merge_sections
  1205. #define _bfd_ecoff_bfd_is_group_section bfd_generic_is_group_section
  1206. #define _bfd_ecoff_bfd_discard_group bfd_generic_discard_group
  1207. #define _bfd_ecoff_section_already_linked \
  1208. _bfd_coff_section_already_linked
  1209. #define _bfd_ecoff_bfd_define_common_symbol bfd_generic_define_common_symbol
  1210. extern const bfd_target mips_ecoff_be_vec;
  1211. const bfd_target mips_ecoff_le_vec =
  1212. {
  1213. "ecoff-littlemips", /* name */
  1214. bfd_target_ecoff_flavour,
  1215. BFD_ENDIAN_LITTLE, /* data byte order is little */
  1216. BFD_ENDIAN_LITTLE, /* header byte order is little */
  1217. (HAS_RELOC | EXEC_P | /* object flags */
  1218. HAS_LINENO | HAS_DEBUG |
  1219. HAS_SYMS | HAS_LOCALS | WP_TEXT | D_PAGED),
  1220. (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC | SEC_CODE | SEC_DATA),
  1221. 0, /* leading underscore */
  1222. ' ', /* ar_pad_char */
  1223. 15, /* ar_max_namelen */
  1224. 0, /* match priority. */
  1225. bfd_getl64, bfd_getl_signed_64, bfd_putl64,
  1226. bfd_getl32, bfd_getl_signed_32, bfd_putl32,
  1227. bfd_getl16, bfd_getl_signed_16, bfd_putl16, /* data */
  1228. bfd_getl64, bfd_getl_signed_64, bfd_putl64,
  1229. bfd_getl32, bfd_getl_signed_32, bfd_putl32,
  1230. bfd_getl16, bfd_getl_signed_16, bfd_putl16, /* hdrs */
  1231. {_bfd_dummy_target, coff_object_p, /* bfd_check_format */
  1232. bfd_generic_archive_p, _bfd_dummy_target},
  1233. {bfd_false, _bfd_ecoff_mkobject, /* bfd_set_format */
  1234. _bfd_generic_mkarchive, bfd_false},
  1235. {bfd_false, _bfd_ecoff_write_object_contents, /* bfd_write_contents */
  1236. _bfd_write_archive_contents, bfd_false},
  1237. BFD_JUMP_TABLE_GENERIC (_bfd_ecoff),
  1238. BFD_JUMP_TABLE_COPY (_bfd_ecoff),
  1239. BFD_JUMP_TABLE_CORE (_bfd_nocore),
  1240. BFD_JUMP_TABLE_ARCHIVE (_bfd_ecoff),
  1241. BFD_JUMP_TABLE_SYMBOLS (_bfd_ecoff),
  1242. BFD_JUMP_TABLE_RELOCS (_bfd_ecoff),
  1243. BFD_JUMP_TABLE_WRITE (_bfd_ecoff),
  1244. BFD_JUMP_TABLE_LINK (_bfd_ecoff),
  1245. BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
  1246. & mips_ecoff_be_vec,
  1247. & mips_ecoff_backend_data
  1248. };
  1249. const bfd_target mips_ecoff_be_vec =
  1250. {
  1251. "ecoff-bigmips", /* name */
  1252. bfd_target_ecoff_flavour,
  1253. BFD_ENDIAN_BIG, /* data byte order is big */
  1254. BFD_ENDIAN_BIG, /* header byte order is big */
  1255. (HAS_RELOC | EXEC_P | /* object flags */
  1256. HAS_LINENO | HAS_DEBUG |
  1257. HAS_SYMS | HAS_LOCALS | WP_TEXT | D_PAGED),
  1258. (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC | SEC_CODE | SEC_DATA),
  1259. 0, /* leading underscore */
  1260. ' ', /* ar_pad_char */
  1261. 15, /* ar_max_namelen */
  1262. 0, /* match priority. */
  1263. bfd_getb64, bfd_getb_signed_64, bfd_putb64,
  1264. bfd_getb32, bfd_getb_signed_32, bfd_putb32,
  1265. bfd_getb16, bfd_getb_signed_16, bfd_putb16,
  1266. bfd_getb64, bfd_getb_signed_64, bfd_putb64,
  1267. bfd_getb32, bfd_getb_signed_32, bfd_putb32,
  1268. bfd_getb16, bfd_getb_signed_16, bfd_putb16,
  1269. {_bfd_dummy_target, coff_object_p, /* bfd_check_format */
  1270. bfd_generic_archive_p, _bfd_dummy_target},
  1271. {bfd_false, _bfd_ecoff_mkobject, /* bfd_set_format */
  1272. _bfd_generic_mkarchive, bfd_false},
  1273. {bfd_false, _bfd_ecoff_write_object_contents, /* bfd_write_contents */
  1274. _bfd_write_archive_contents, bfd_false},
  1275. BFD_JUMP_TABLE_GENERIC (_bfd_ecoff),
  1276. BFD_JUMP_TABLE_COPY (_bfd_ecoff),
  1277. BFD_JUMP_TABLE_CORE (_bfd_nocore),
  1278. BFD_JUMP_TABLE_ARCHIVE (_bfd_ecoff),
  1279. BFD_JUMP_TABLE_SYMBOLS (_bfd_ecoff),
  1280. BFD_JUMP_TABLE_RELOCS (_bfd_ecoff),
  1281. BFD_JUMP_TABLE_WRITE (_bfd_ecoff),
  1282. BFD_JUMP_TABLE_LINK (_bfd_ecoff),
  1283. BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
  1284. & mips_ecoff_le_vec,
  1285. & mips_ecoff_backend_data
  1286. };
  1287. const bfd_target mips_ecoff_bele_vec =
  1288. {
  1289. "ecoff-biglittlemips", /* name */
  1290. bfd_target_ecoff_flavour,
  1291. BFD_ENDIAN_LITTLE, /* data byte order is little */
  1292. BFD_ENDIAN_BIG, /* header byte order is big */
  1293. (HAS_RELOC | EXEC_P | /* object flags */
  1294. HAS_LINENO | HAS_DEBUG |
  1295. HAS_SYMS | HAS_LOCALS | WP_TEXT | D_PAGED),
  1296. (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC | SEC_CODE | SEC_DATA),
  1297. 0, /* leading underscore */
  1298. ' ', /* ar_pad_char */
  1299. 15, /* ar_max_namelen */
  1300. 0, /* match priority. */
  1301. bfd_getl64, bfd_getl_signed_64, bfd_putl64,
  1302. bfd_getl32, bfd_getl_signed_32, bfd_putl32,
  1303. bfd_getl16, bfd_getl_signed_16, bfd_putl16, /* data */
  1304. bfd_getb64, bfd_getb_signed_64, bfd_putb64,
  1305. bfd_getb32, bfd_getb_signed_32, bfd_putb32,
  1306. bfd_getb16, bfd_getb_signed_16, bfd_putb16, /* hdrs */
  1307. {_bfd_dummy_target, coff_object_p, /* bfd_check_format */
  1308. bfd_generic_archive_p, _bfd_dummy_target},
  1309. {bfd_false, _bfd_ecoff_mkobject, /* bfd_set_format */
  1310. _bfd_generic_mkarchive, bfd_false},
  1311. {bfd_false, _bfd_ecoff_write_object_contents, /* bfd_write_contents */
  1312. _bfd_write_archive_contents, bfd_false},
  1313. BFD_JUMP_TABLE_GENERIC (_bfd_ecoff),
  1314. BFD_JUMP_TABLE_COPY (_bfd_ecoff),
  1315. BFD_JUMP_TABLE_CORE (_bfd_nocore),
  1316. BFD_JUMP_TABLE_ARCHIVE (_bfd_ecoff),
  1317. BFD_JUMP_TABLE_SYMBOLS (_bfd_ecoff),
  1318. BFD_JUMP_TABLE_RELOCS (_bfd_ecoff),
  1319. BFD_JUMP_TABLE_WRITE (_bfd_ecoff),
  1320. BFD_JUMP_TABLE_LINK (_bfd_ecoff),
  1321. BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
  1322. NULL,
  1323. & mips_ecoff_backend_data
  1324. };