coff-mips.c 47 KB

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