mipsbsd.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  1. /* BFD backend for MIPS BSD (a.out) binaries.
  2. Copyright (C) 1993-2015 Free Software Foundation, Inc.
  3. Written by Ralph Campbell.
  4. This file is part of BFD, the Binary File Descriptor library.
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 3 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
  16. MA 02110-1301, USA. */
  17. /* #define ENTRY_CAN_BE_ZERO */
  18. #define N_HEADER_IN_TEXT(x) 1
  19. #define N_TXTADDR(x) \
  20. (N_MAGIC(x) != ZMAGIC ? (x).a_entry : /* object file or NMAGIC */\
  21. TEXT_START_ADDR + EXEC_BYTES_SIZE /* no padding */\
  22. )
  23. #define N_DATADDR(x) (N_TXTADDR(x)+N_TXTSIZE(x))
  24. #define TEXT_START_ADDR 4096
  25. #define TARGET_PAGE_SIZE 4096
  26. #define SEGMENT_SIZE TARGET_PAGE_SIZE
  27. #define DEFAULT_ARCH bfd_arch_mips
  28. #define MACHTYPE_OK(mtype) ((mtype) == M_UNKNOWN \
  29. || (mtype) == M_MIPS1 || (mtype) == M_MIPS2)
  30. #define MY_symbol_leading_char '\0'
  31. /* Do not "beautify" the CONCAT* macro args. Traditional C will not
  32. remove whitespace added here, and thus will fail to concatenate
  33. the tokens. */
  34. #define MY(OP) CONCAT2 (mipsbsd_,OP)
  35. #include "sysdep.h"
  36. #include "bfd.h"
  37. #include "libbfd.h"
  38. #include "libaout.h"
  39. #define SET_ARCH_MACH(ABFD, EXEC) \
  40. MY(set_arch_mach) (ABFD, N_MACHTYPE (EXEC)); \
  41. MY(choose_reloc_size) (ABFD);
  42. static void MY(set_arch_mach) (bfd *, unsigned long);
  43. static void MY(choose_reloc_size) (bfd *);
  44. #define MY_write_object_contents MY(write_object_contents)
  45. static bfd_boolean MY(write_object_contents) (bfd *);
  46. /* We can't use MY(x) here because it leads to a recursive call to CONCAT2
  47. when expanded inside JUMP_TABLE. */
  48. #define MY_bfd_reloc_type_lookup mipsbsd_reloc_type_lookup
  49. #define MY_bfd_reloc_name_lookup mipsbsd_reloc_name_lookup
  50. #define MY_canonicalize_reloc mipsbsd_canonicalize_reloc
  51. #define MY_bfd_link_hash_table_create _bfd_generic_link_hash_table_create
  52. #define MY_bfd_link_add_symbols _bfd_generic_link_add_symbols
  53. #define MY_final_link_callback unused
  54. #define MY_bfd_final_link _bfd_generic_final_link
  55. #define MY_backend_data &MY(backend_data)
  56. #define MY_BFD_TARGET
  57. #include "aout-target.h"
  58. static bfd_reloc_status_type mips_fix_jmp_addr
  59. (bfd *, arelent *, struct bfd_symbol *, void *, asection *,
  60. bfd *, char **);
  61. long MY(canonicalize_reloc) (bfd *, sec_ptr, arelent **, asymbol **);
  62. static void
  63. MY(set_arch_mach) (bfd *abfd, unsigned long machtype)
  64. {
  65. enum bfd_architecture arch;
  66. unsigned int machine;
  67. /* Determine the architecture and machine type of the object file. */
  68. switch (machtype)
  69. {
  70. case M_MIPS1:
  71. arch = bfd_arch_mips;
  72. machine = bfd_mach_mips3000;
  73. break;
  74. case M_MIPS2:
  75. arch = bfd_arch_mips;
  76. machine = bfd_mach_mips4000;
  77. break;
  78. default:
  79. arch = bfd_arch_obscure;
  80. machine = 0;
  81. break;
  82. }
  83. bfd_set_arch_mach (abfd, arch, machine);
  84. }
  85. /* Determine the size of a relocation entry, based on the architecture */
  86. static void
  87. MY (choose_reloc_size) (bfd *abfd)
  88. {
  89. switch (bfd_get_arch (abfd))
  90. {
  91. case bfd_arch_sparc:
  92. case bfd_arch_mips:
  93. obj_reloc_entry_size (abfd) = RELOC_EXT_SIZE;
  94. break;
  95. default:
  96. obj_reloc_entry_size (abfd) = RELOC_STD_SIZE;
  97. break;
  98. }
  99. }
  100. /* Write an object file in BSD a.out format.
  101. Section contents have already been written. We write the
  102. file header, symbols, and relocation. */
  103. static bfd_boolean
  104. MY (write_object_contents) (bfd *abfd)
  105. {
  106. struct external_exec exec_bytes;
  107. struct internal_exec *execp = exec_hdr (abfd);
  108. /* Magic number, maestro, please! */
  109. switch (bfd_get_arch (abfd))
  110. {
  111. case bfd_arch_m68k:
  112. switch (bfd_get_mach (abfd))
  113. {
  114. case bfd_mach_m68010:
  115. N_SET_MACHTYPE (*execp, M_68010);
  116. break;
  117. default:
  118. case bfd_mach_m68020:
  119. N_SET_MACHTYPE (*execp, M_68020);
  120. break;
  121. }
  122. break;
  123. case bfd_arch_sparc:
  124. N_SET_MACHTYPE (*execp, M_SPARC);
  125. break;
  126. case bfd_arch_i386:
  127. N_SET_MACHTYPE (*execp, M_386);
  128. break;
  129. case bfd_arch_mips:
  130. switch (bfd_get_mach (abfd))
  131. {
  132. case bfd_mach_mips4000:
  133. case bfd_mach_mips6000:
  134. N_SET_MACHTYPE (*execp, M_MIPS2);
  135. break;
  136. default:
  137. N_SET_MACHTYPE (*execp, M_MIPS1);
  138. break;
  139. }
  140. break;
  141. default:
  142. N_SET_MACHTYPE (*execp, M_UNKNOWN);
  143. }
  144. MY (choose_reloc_size) (abfd);
  145. WRITE_HEADERS (abfd, execp);
  146. return TRUE;
  147. }
  148. /* MIPS relocation types. */
  149. #define MIPS_RELOC_32 0
  150. #define MIPS_RELOC_JMP 1
  151. #define MIPS_RELOC_WDISP16 2
  152. #define MIPS_RELOC_HI16 3
  153. #define MIPS_RELOC_HI16_S 4
  154. #define MIPS_RELOC_LO16 5
  155. /* This is only called when performing a BFD_RELOC_MIPS_JMP relocation.
  156. The jump destination address is formed from the upper 4 bits of the
  157. "current" program counter concatenated with the jump instruction's
  158. 26 bit field and two trailing zeros.
  159. If the destination address is not in the same segment as the "current"
  160. program counter, then we need to signal an error. */
  161. static bfd_reloc_status_type
  162. mips_fix_jmp_addr (bfd *abfd ATTRIBUTE_UNUSED,
  163. arelent *reloc_entry,
  164. struct bfd_symbol *symbol,
  165. void * data ATTRIBUTE_UNUSED,
  166. asection *input_section,
  167. bfd *output_bfd,
  168. char **error_message ATTRIBUTE_UNUSED)
  169. {
  170. bfd_vma relocation, pc;
  171. /* If this is a partial relocation, just continue. */
  172. if (output_bfd != (bfd *) NULL)
  173. return bfd_reloc_continue;
  174. /* If this is an undefined symbol, return error */
  175. if (bfd_is_und_section (symbol->section)
  176. && (symbol->flags & BSF_WEAK) == 0)
  177. return bfd_reloc_undefined;
  178. /* Work out which section the relocation is targeted at and the
  179. initial relocation command value. */
  180. if (bfd_is_com_section (symbol->section))
  181. relocation = 0;
  182. else
  183. relocation = symbol->value;
  184. relocation += symbol->section->output_section->vma;
  185. relocation += symbol->section->output_offset;
  186. relocation += reloc_entry->addend;
  187. pc = input_section->output_section->vma + input_section->output_offset +
  188. reloc_entry->address + 4;
  189. if ((relocation & 0xF0000000) != (pc & 0xF0000000))
  190. return bfd_reloc_overflow;
  191. return bfd_reloc_continue;
  192. }
  193. /* This is only called when performing a BFD_RELOC_HI16_S relocation.
  194. We need to see if bit 15 is set in the result. If it is, we add
  195. 0x10000 and continue normally. This will compensate for the sign extension
  196. when the low bits are added at run time. */
  197. static bfd_reloc_status_type
  198. mips_fix_hi16_s (bfd *abfd ATTRIBUTE_UNUSED,
  199. arelent *reloc_entry,
  200. asymbol *symbol,
  201. void * data ATTRIBUTE_UNUSED,
  202. asection *input_section ATTRIBUTE_UNUSED,
  203. bfd *output_bfd,
  204. char **error_message ATTRIBUTE_UNUSED)
  205. {
  206. bfd_vma relocation;
  207. /* If this is a partial relocation, just continue. */
  208. if (output_bfd != (bfd *)NULL)
  209. return bfd_reloc_continue;
  210. /* If this is an undefined symbol, return error. */
  211. if (bfd_is_und_section (symbol->section)
  212. && (symbol->flags & BSF_WEAK) == 0)
  213. return bfd_reloc_undefined;
  214. /* Work out which section the relocation is targeted at and the
  215. initial relocation command value. */
  216. if (bfd_is_com_section (symbol->section))
  217. relocation = 0;
  218. else
  219. relocation = symbol->value;
  220. relocation += symbol->section->output_section->vma;
  221. relocation += symbol->section->output_offset;
  222. relocation += reloc_entry->addend;
  223. if (relocation & 0x8000)
  224. reloc_entry->addend += 0x10000;
  225. return bfd_reloc_continue;
  226. }
  227. static reloc_howto_type mips_howto_table_ext[] =
  228. {
  229. {MIPS_RELOC_32, 0, 2, 32, FALSE, 0, complain_overflow_bitfield, 0,
  230. "32", FALSE, 0, 0xffffffff, FALSE},
  231. {MIPS_RELOC_JMP, 2, 2, 26, FALSE, 0, complain_overflow_dont,
  232. mips_fix_jmp_addr,
  233. "MIPS_JMP", FALSE, 0, 0x03ffffff, FALSE},
  234. {MIPS_RELOC_WDISP16, 2, 2, 16, TRUE, 0, complain_overflow_signed, 0,
  235. "WDISP16", FALSE, 0, 0x0000ffff, FALSE},
  236. {MIPS_RELOC_HI16, 16, 2, 16, FALSE, 0, complain_overflow_bitfield, 0,
  237. "HI16", FALSE, 0, 0x0000ffff, FALSE},
  238. {MIPS_RELOC_HI16_S, 16, 2, 16, FALSE, 0, complain_overflow_bitfield,
  239. mips_fix_hi16_s,
  240. "HI16_S", FALSE, 0, 0x0000ffff, FALSE},
  241. {MIPS_RELOC_LO16, 0, 2, 16, FALSE, 0, complain_overflow_dont, 0,
  242. "LO16", FALSE, 0, 0x0000ffff, FALSE},
  243. };
  244. static reloc_howto_type *
  245. MY(reloc_type_lookup) (bfd *abfd, bfd_reloc_code_real_type code)
  246. {
  247. if (bfd_get_arch (abfd) != bfd_arch_mips)
  248. return NULL;
  249. switch (code)
  250. {
  251. case BFD_RELOC_CTOR:
  252. case BFD_RELOC_32:
  253. return (&mips_howto_table_ext[MIPS_RELOC_32]);
  254. case BFD_RELOC_MIPS_JMP:
  255. return (&mips_howto_table_ext[MIPS_RELOC_JMP]);
  256. case BFD_RELOC_16_PCREL_S2:
  257. return (&mips_howto_table_ext[MIPS_RELOC_WDISP16]);
  258. case BFD_RELOC_HI16:
  259. return (&mips_howto_table_ext[MIPS_RELOC_HI16]);
  260. case BFD_RELOC_HI16_S:
  261. return (&mips_howto_table_ext[MIPS_RELOC_HI16_S]);
  262. case BFD_RELOC_LO16:
  263. return (&mips_howto_table_ext[MIPS_RELOC_LO16]);
  264. default:
  265. return NULL;
  266. }
  267. }
  268. static reloc_howto_type *
  269. MY(reloc_name_lookup) (bfd *abfd ATTRIBUTE_UNUSED,
  270. const char *r_name)
  271. {
  272. unsigned int i;
  273. for (i = 0;
  274. i < sizeof (mips_howto_table_ext) / sizeof (mips_howto_table_ext[0]);
  275. i++)
  276. if (mips_howto_table_ext[i].name != NULL
  277. && strcasecmp (mips_howto_table_ext[i].name, r_name) == 0)
  278. return &mips_howto_table_ext[i];
  279. return NULL;
  280. }
  281. /* This is just like the standard aoutx.h version but we need to do our
  282. own mapping of external reloc type values to howto entries. */
  283. long
  284. MY(canonicalize_reloc) (bfd *abfd,
  285. sec_ptr section,
  286. arelent **relptr,
  287. asymbol **symbols)
  288. {
  289. arelent *tblptr = section->relocation;
  290. unsigned int count, c;
  291. extern reloc_howto_type NAME(aout,ext_howto_table)[];
  292. /* If we have already read in the relocation table, return the values. */
  293. if (section->flags & SEC_CONSTRUCTOR)
  294. {
  295. arelent_chain *chain = section->constructor_chain;
  296. for (count = 0; count < section->reloc_count; count++)
  297. {
  298. *relptr++ = &chain->relent;
  299. chain = chain->next;
  300. }
  301. *relptr = 0;
  302. return section->reloc_count;
  303. }
  304. if (tblptr && section->reloc_count)
  305. {
  306. for (count = 0; count++ < section->reloc_count;)
  307. *relptr++ = tblptr++;
  308. *relptr = 0;
  309. return section->reloc_count;
  310. }
  311. if (!NAME(aout,slurp_reloc_table) (abfd, section, symbols))
  312. return -1;
  313. tblptr = section->relocation;
  314. /* fix up howto entries. */
  315. for (count = 0; count++ < section->reloc_count;)
  316. {
  317. c = tblptr->howto - NAME(aout,ext_howto_table);
  318. tblptr->howto = &mips_howto_table_ext[c];
  319. *relptr++ = tblptr++;
  320. }
  321. *relptr = 0;
  322. return section->reloc_count;
  323. }
  324. static const struct aout_backend_data MY(backend_data) =
  325. {
  326. 0, /* zmagic contiguous */
  327. 1, /* text incl header */
  328. 0, /* entry is text address */
  329. 0, /* exec_hdr_flags */
  330. TARGET_PAGE_SIZE, /* text vma */
  331. MY_set_sizes,
  332. 0, /* text size includes exec header */
  333. 0, /* add_dynamic_symbols */
  334. 0, /* add_one_symbol */
  335. 0, /* link_dynamic_object */
  336. 0, /* write_dynamic_symbol */
  337. 0, /* check_dynamic_reloc */
  338. 0 /* finish_dynamic_link */
  339. };
  340. extern const bfd_target mips_aout_be_vec;
  341. const bfd_target mips_aout_le_vec =
  342. {
  343. "a.out-mips-little", /* name */
  344. bfd_target_aout_flavour,
  345. BFD_ENDIAN_LITTLE, /* target byte order (little) */
  346. BFD_ENDIAN_LITTLE, /* target headers byte order (little) */
  347. (HAS_RELOC | EXEC_P | /* object flags */
  348. HAS_LINENO | HAS_DEBUG |
  349. HAS_SYMS | HAS_LOCALS | WP_TEXT | D_PAGED),
  350. (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC | SEC_CODE | SEC_DATA),
  351. MY_symbol_leading_char,
  352. ' ', /* ar_pad_char */
  353. 15, /* ar_max_namelen */
  354. 0, /* match priority. */
  355. bfd_getl64, bfd_getl_signed_64, bfd_putl64,
  356. bfd_getl32, bfd_getl_signed_32, bfd_putl32,
  357. bfd_getl16, bfd_getl_signed_16, bfd_putl16, /* data */
  358. bfd_getl64, bfd_getl_signed_64, bfd_putl64,
  359. bfd_getl32, bfd_getl_signed_32, bfd_putl32,
  360. bfd_getl16, bfd_getl_signed_16, bfd_putl16, /* hdrs */
  361. {_bfd_dummy_target, MY_object_p, /* bfd_check_format */
  362. bfd_generic_archive_p, MY_core_file_p},
  363. {bfd_false, MY_mkobject, /* bfd_set_format */
  364. _bfd_generic_mkarchive, bfd_false},
  365. {bfd_false, MY_write_object_contents, /* bfd_write_contents */
  366. _bfd_write_archive_contents, bfd_false},
  367. BFD_JUMP_TABLE_GENERIC (MY),
  368. BFD_JUMP_TABLE_COPY (MY),
  369. BFD_JUMP_TABLE_CORE (MY),
  370. BFD_JUMP_TABLE_ARCHIVE (MY),
  371. BFD_JUMP_TABLE_SYMBOLS (MY),
  372. BFD_JUMP_TABLE_RELOCS (MY),
  373. BFD_JUMP_TABLE_WRITE (MY),
  374. BFD_JUMP_TABLE_LINK (MY),
  375. BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
  376. & mips_aout_be_vec,
  377. MY_backend_data
  378. };
  379. const bfd_target mips_aout_be_vec =
  380. {
  381. "a.out-mips-big", /* name */
  382. bfd_target_aout_flavour,
  383. BFD_ENDIAN_BIG, /* target byte order (big) */
  384. BFD_ENDIAN_BIG, /* target headers byte order (big) */
  385. (HAS_RELOC | EXEC_P | /* object flags */
  386. HAS_LINENO | HAS_DEBUG |
  387. HAS_SYMS | HAS_LOCALS | WP_TEXT | D_PAGED),
  388. (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC | SEC_CODE | SEC_DATA),
  389. MY_symbol_leading_char,
  390. ' ', /* ar_pad_char */
  391. 15, /* ar_max_namelen */
  392. 0, /* match priority. */
  393. bfd_getb64, bfd_getb_signed_64, bfd_putb64,
  394. bfd_getb32, bfd_getb_signed_32, bfd_putb32,
  395. bfd_getb16, bfd_getb_signed_16, bfd_putb16, /* data */
  396. bfd_getb64, bfd_getb_signed_64, bfd_putb64,
  397. bfd_getb32, bfd_getb_signed_32, bfd_putb32,
  398. bfd_getb16, bfd_getb_signed_16, bfd_putb16, /* hdrs */
  399. {_bfd_dummy_target, MY_object_p, /* bfd_check_format */
  400. bfd_generic_archive_p, MY_core_file_p},
  401. {bfd_false, MY_mkobject, /* bfd_set_format */
  402. _bfd_generic_mkarchive, bfd_false},
  403. {bfd_false, MY_write_object_contents, /* bfd_write_contents */
  404. _bfd_write_archive_contents, bfd_false},
  405. BFD_JUMP_TABLE_GENERIC (MY),
  406. BFD_JUMP_TABLE_COPY (MY),
  407. BFD_JUMP_TABLE_CORE (MY),
  408. BFD_JUMP_TABLE_ARCHIVE (MY),
  409. BFD_JUMP_TABLE_SYMBOLS (MY),
  410. BFD_JUMP_TABLE_RELOCS (MY),
  411. BFD_JUMP_TABLE_WRITE (MY),
  412. BFD_JUMP_TABLE_LINK (MY),
  413. BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
  414. & mips_aout_le_vec,
  415. MY_backend_data
  416. };