aout-cris.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. /* BFD backend for CRIS a.out binaries.
  2. Copyright (C) 2000-2022 Free Software Foundation, Inc.
  3. Contributed by Axis Communications AB.
  4. Written by Hans-Peter Nilsson.
  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. /* See info in the file PORTING for documentation of these macros and
  19. functions. Beware; some of the information there is outdated. */
  20. #define N_HEADER_IN_TEXT(x) 0
  21. #define N_TXTOFF(x) 32
  22. #define ENTRY_CAN_BE_ZERO
  23. #define TEXT_START_ADDR 0
  24. /* Without reading symbols to get the text start symbol, there is no way
  25. to know where the text segment starts in an a.out file. Defaulting to
  26. anything as constant as TEXT_START_ADDR is bad. But we can guess from
  27. the entry point, which is usually within the first 64k of the text
  28. segment. We also assume here that the text segment is 64k-aligned.
  29. FIXME: It is also wrong to assume that data and bss follow immediately
  30. after text, but with those, we don't have any choice besides reading
  31. symbol info, and luckily there's no pressing need for correctness for
  32. those vma:s at this time. */
  33. #define N_TXTADDR(x) ((x)->a_entry & ~(bfd_vma) 0xffff)
  34. /* If you change this to 4, you can not link to an address N*4+2. */
  35. #define SEGMENT_SIZE 2
  36. /* For some reason, if the a.out file has Z_MAGIC, then
  37. adata(abfd).exec_bytes_size is not used, but rather
  38. adata(abfd).zmagic_disk_block_size, even though the exec_header is
  39. *not* included in the text segment. A simple workaround is to
  40. #define ZMAGIC_DISK_BLOCK_SIZE, which is used if defined; otherwise
  41. TARGET_PAGE_SIZE is used. */
  42. #define ZMAGIC_DISK_BLOCK_SIZE N_TXTOFF (0)
  43. /* It seems odd at first to set a page-size this low, but gives greater
  44. freedom in where things can be linked. The drawback is that you have
  45. to set alignment and padding in linker scripts. */
  46. #define TARGET_PAGE_SIZE SEGMENT_SIZE
  47. #define TARGETNAME "a.out-cris"
  48. /* Do not "beautify" the CONCAT* macro args. Traditional C will not
  49. remove whitespace added here, and thus will fail to concatenate
  50. the tokens. */
  51. #define MY(OP) CONCAT2 (cris_aout_,OP)
  52. #define NAME(x, y) CONCAT3 (cris_aout,_32_,y)
  53. #include "sysdep.h"
  54. #include "bfd.h"
  55. /* Version 1 of the header. */
  56. #define MY_exec_hdr_flags 1
  57. #define MY_write_object_contents MY (write_object_contents)
  58. static bool MY (write_object_contents) (bfd *);
  59. /* Forward this, so we can use a pointer to it in PARAMS. */
  60. struct reloc_ext_external;
  61. #define MY_swap_ext_reloc_out MY (swap_ext_reloc_out)
  62. static void MY (swap_ext_reloc_out) (bfd *, arelent *, struct reloc_ext_external *);
  63. #define MY_swap_ext_reloc_in MY (swap_ext_reloc_in)
  64. static void MY (swap_ext_reloc_in) (bfd *, struct reloc_ext_external *,
  65. arelent *, asymbol **, bfd_size_type);
  66. #define MY_set_sizes MY (set_sizes)
  67. static bool MY (set_sizes) (bfd *);
  68. /* To set back reloc_size to ext, we make MY (set_sizes) be called
  69. through this construct. Note that MY_set_arch_mach is only called
  70. through SET_ARCH_MACH. The default bfd_default_set_arch_mach will
  71. not call set_sizes. */
  72. #define SET_ARCH_MACH(BFD, EXECP) \
  73. bfd_set_arch_mach (BFD, bfd_arch_cris, N_MACHTYPE (EXECP))
  74. /* These macros describe the binary layout of the reloc information we
  75. use in a file. */
  76. #define RELOC_EXT_BITS_EXTERN_LITTLE 0x80
  77. #define RELOC_EXT_BITS_TYPE_LITTLE 3
  78. #define RELOC_EXT_BITS_TYPE_SH_LITTLE 0
  79. #ifndef MY_get_section_contents
  80. #define MY_get_section_contents aout_32_get_section_contents
  81. #endif
  82. #define MACHTYPE_OK(mtype) ((mtype) == M_CRIS)
  83. /* Include generic functions (some are overridden above). */
  84. #include "aout32.c"
  85. #include "aout-target.h"
  86. /* We need our own version to set header flags. */
  87. static bool
  88. MY (write_object_contents) (bfd *abfd)
  89. {
  90. struct external_exec exec_bytes;
  91. struct internal_exec *execp = exec_hdr (abfd);
  92. /* We set the reloc type to RELOC_EXT_SIZE, although setting it at all
  93. seems unnecessary when inspecting as and ld behavior (not an
  94. exhaustive inspection). The default write_object_contents
  95. definition sets RELOC_EXT_SIZE, so we follow suite and set it too. */
  96. obj_reloc_entry_size (abfd) = RELOC_EXT_SIZE;
  97. /* Setting N_SET_MACHTYPE and using N_SET_FLAGS is not performed by
  98. the default definition. */
  99. if (bfd_get_arch (abfd) == bfd_arch_cris)
  100. N_SET_MACHTYPE (execp, M_CRIS);
  101. N_SET_FLAGS (execp, aout_backend_info (abfd)->exec_hdr_flags);
  102. WRITE_HEADERS (abfd, execp);
  103. return true;
  104. }
  105. /* We need our own for these reasons:
  106. - Assert that a normal 8, 16 or 32 reloc is output.
  107. - Fix what seems to be a weak-bug (perhaps there for valid reasons). */
  108. static void
  109. MY (swap_ext_reloc_out) (bfd *abfd,
  110. arelent *g,
  111. struct reloc_ext_external *natptr)
  112. {
  113. int r_index;
  114. int r_extern;
  115. unsigned int r_type;
  116. bfd_vma r_addend;
  117. asymbol *sym = *(g->sym_ptr_ptr);
  118. asection *output_section = sym->section->output_section;
  119. PUT_WORD (abfd, g->address, natptr->r_address);
  120. r_type = (unsigned int) g->howto->type;
  121. r_addend = g->addend;
  122. if ((sym->flags & BSF_SECTION_SYM) != 0)
  123. r_addend += (*(g->sym_ptr_ptr))->section->output_section->vma;
  124. /* If this relocation is relative to a symbol then set the
  125. r_index to the symbols index, and the r_extern bit.
  126. Absolute symbols can come in in two ways, either as an offset
  127. from the abs section, or as a symbol which has an abs value.
  128. check for that here. */
  129. if (bfd_is_abs_section (bfd_asymbol_section (sym)))
  130. {
  131. r_extern = 0;
  132. r_index = N_ABS;
  133. }
  134. else if ((sym->flags & BSF_SECTION_SYM) == 0)
  135. {
  136. if (bfd_is_und_section (bfd_asymbol_section (sym))
  137. /* Remember to check for weak symbols; they count as global. */
  138. || (sym->flags & (BSF_GLOBAL | BSF_WEAK)) != 0)
  139. r_extern = 1;
  140. else
  141. r_extern = 0;
  142. r_index = (*(g->sym_ptr_ptr))->KEEPIT;
  143. }
  144. else
  145. {
  146. /* Just an ordinary section. */
  147. r_extern = 0;
  148. r_index = output_section->target_index;
  149. }
  150. /* The relocation type is the same as the canonical ones, but only
  151. the first 3 are used: RELOC_8, RELOC_16, RELOC_32.
  152. We may change this later, but assert this for the moment. */
  153. if (r_type > 2)
  154. {
  155. /* xgettext:c-format */
  156. _bfd_error_handler (_("%pB: unsupported relocation type exported: %#x"),
  157. abfd, r_type);
  158. bfd_set_error (bfd_error_wrong_format);
  159. }
  160. /* Now the fun stuff. */
  161. natptr->r_index[2] = r_index >> 16;
  162. natptr->r_index[1] = r_index >> 8;
  163. natptr->r_index[0] = r_index;
  164. natptr->r_type[0] =
  165. (r_extern ? RELOC_EXT_BITS_EXTERN_LITTLE : 0)
  166. | (r_type << RELOC_EXT_BITS_TYPE_SH_LITTLE);
  167. PUT_WORD (abfd, r_addend, natptr->r_addend);
  168. }
  169. /* We need our own to assert that a normal 8, 16 or 32 reloc is input. */
  170. static void
  171. MY (swap_ext_reloc_in) (bfd *abfd,
  172. struct reloc_ext_external *bytes,
  173. arelent *cache_ptr,
  174. asymbol **symbols,
  175. bfd_size_type symcount)
  176. {
  177. unsigned int r_index;
  178. int r_extern;
  179. unsigned int r_type;
  180. struct aoutdata *su = &(abfd->tdata.aout_data->a);
  181. cache_ptr->address = (GET_SWORD (abfd, bytes->r_address));
  182. /* Now the fun stuff. */
  183. r_index = (((unsigned int) bytes->r_index[2] << 16)
  184. | ((unsigned int) bytes->r_index[1] << 8)
  185. | bytes->r_index[0]);
  186. r_extern = (0 != (bytes->r_type[0] & RELOC_EXT_BITS_EXTERN_LITTLE));
  187. r_type = ((bytes->r_type[0] & RELOC_EXT_BITS_TYPE_LITTLE)
  188. >> RELOC_EXT_BITS_TYPE_SH_LITTLE);
  189. if (r_type > 2)
  190. {
  191. /* xgettext:c-format */
  192. _bfd_error_handler (_("%pB: unsupported relocation type imported: %#x"),
  193. abfd, r_type);
  194. bfd_set_error (bfd_error_wrong_format);
  195. }
  196. cache_ptr->howto = howto_table_ext + r_type;
  197. if (r_extern && r_index > symcount)
  198. {
  199. _bfd_error_handler
  200. /* xgettext:c-format */
  201. (_("%pB: bad relocation record imported: %d"), abfd, r_index);
  202. bfd_set_error (bfd_error_wrong_format);
  203. /* We continue, so we can catch further errors. */
  204. r_extern = 0;
  205. r_index = N_ABS;
  206. }
  207. /* Magically uses r_extern, symbols etc. Ugly, but it's what's in the
  208. default. */
  209. MOVE_ADDRESS (GET_SWORD (abfd, bytes->r_addend));
  210. }
  211. /* We use the same as the default, except that we also set
  212. "obj_reloc_entry_size (abfd) = RELOC_EXT_SIZE;", to avoid changing
  213. NAME (aout, set_arch_mach) in aoutx. */
  214. static bool
  215. MY (set_sizes) (bfd *abfd)
  216. {
  217. /* Just as the default in aout-target.h (with some #ifdefs folded)... */
  218. adata (abfd).page_size = TARGET_PAGE_SIZE;
  219. adata (abfd).segment_size = SEGMENT_SIZE;
  220. adata (abfd).zmagic_disk_block_size = ZMAGIC_DISK_BLOCK_SIZE;
  221. adata (abfd).exec_bytes_size = EXEC_BYTES_SIZE;
  222. /* ... except for that we have the extended reloc. The alternative
  223. would be to add a check on bfd_arch_cris in NAME (aout,
  224. set_arch_mach) in aoutx.h, but I don't want to do that since
  225. target-specific things should not be added there. */
  226. obj_reloc_entry_size (abfd) = RELOC_EXT_SIZE;
  227. return true;
  228. }