riscix.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656
  1. /* BFD back-end for RISC iX (Acorn, arm) binaries.
  2. Copyright (C) 1994-2015 Free Software Foundation, Inc.
  3. Contributed by Richard Earnshaw (rwe@pegasus.esprit.ec.org)
  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. /* RISC iX overloads the MAGIC field to indicate more than just the usual
  18. [ZNO]MAGIC values. Also included are squeezing information and
  19. shared library usage. */
  20. /* The following come from the man page. */
  21. #define SHLIBLEN 60
  22. #define MF_IMPURE 00200
  23. #define MF_SQUEEZED 01000
  24. #define MF_USES_SL 02000
  25. #define MF_IS_SL 04000
  26. /* Common combinations. */
  27. /* Demand load (impure text). */
  28. #define IMAGIC (MF_IMPURE | ZMAGIC)
  29. /* OMAGIC with large header.
  30. May contain a ref to a shared lib required by the object. */
  31. #define SPOMAGIC (MF_USES_SL | OMAGIC)
  32. /* A reference to a shared library.
  33. The text portion of the object contains "overflow text" from
  34. the shared library to be linked in with an object. */
  35. #define SLOMAGIC (MF_IS_SL | OMAGIC)
  36. /* Sqeezed demand paged.
  37. NOTE: This interpretation of QMAGIC seems to be at variance
  38. with that used on other architectures. */
  39. #define QMAGIC (MF_SQUEEZED | ZMAGIC)
  40. /* Program which uses sl. */
  41. #define SPZMAGIC (MF_USES_SL | ZMAGIC)
  42. /* Sqeezed ditto. */
  43. #define SPQMAGIC (MF_USES_SL | QMAGIC)
  44. /* Shared lib part of prog. */
  45. #define SLZMAGIC (MF_IS_SL | ZMAGIC)
  46. /* Sl which uses another. */
  47. #define SLPZMAGIC (MF_USES_SL | SLZMAGIC)
  48. #define N_SHARED_LIB(x) ((x).a_info & MF_USES_SL)
  49. /* Only a pure OMAGIC file has the minimal header. */
  50. #define N_TXTOFF(x) \
  51. ((x).a_info == OMAGIC \
  52. ? 32 \
  53. : (N_MAGIC(x) == ZMAGIC \
  54. ? TARGET_PAGE_SIZE \
  55. : 999))
  56. #define N_TXTADDR(x) \
  57. (N_MAGIC(x) != ZMAGIC \
  58. ? (bfd_vma) 0 /* object file or NMAGIC */ \
  59. /* Programs with shared libs are loaded at the first page after all the \
  60. text segments of the shared library programs. Without looking this \
  61. up we can't know exactly what the address will be. A reasonable guess \
  62. is that a_entry will be in the first page of the executable. */ \
  63. : (N_SHARED_LIB(x) \
  64. ? ((x).a_entry & ~(bfd_vma) (TARGET_PAGE_SIZE - 1)) \
  65. : (bfd_vma) TEXT_START_ADDR))
  66. #define N_SYMOFF(x) \
  67. (N_TXTOFF (x) + (x).a_text + (x).a_data + (x).a_trsize + (x).a_drsize)
  68. #define N_STROFF(x) (N_SYMOFF (x) + (x).a_syms)
  69. #define TEXT_START_ADDR 32768
  70. #define TARGET_PAGE_SIZE 32768
  71. #define SEGMENT_SIZE TARGET_PAGE_SIZE
  72. #define DEFAULT_ARCH bfd_arch_arm
  73. /* Do not "beautify" the CONCAT* macro args. Traditional C will not
  74. remove whitespace added here, and thus will fail to concatenate
  75. the tokens. */
  76. #define MY(OP) CONCAT2 (arm_aout_riscix_,OP)
  77. #define TARGETNAME "a.out-riscix"
  78. #define N_BADMAG(x) ((((x).a_info & ~007200) != ZMAGIC) \
  79. && (((x).a_info & ~006000) != OMAGIC) \
  80. && ((x).a_info != NMAGIC))
  81. #define N_MAGIC(x) ((x).a_info & ~07200)
  82. #include "sysdep.h"
  83. #include "bfd.h"
  84. #include "libbfd.h"
  85. #define WRITE_HEADERS(abfd, execp) \
  86. { \
  87. bfd_size_type text_size; /* Dummy vars. */ \
  88. file_ptr text_end; \
  89. \
  90. if (adata (abfd).magic == undecided_magic) \
  91. NAME (aout, adjust_sizes_and_vmas) (abfd, & text_size, & text_end); \
  92. \
  93. execp->a_syms = bfd_get_symcount (abfd) * EXTERNAL_NLIST_SIZE; \
  94. execp->a_entry = bfd_get_start_address (abfd); \
  95. \
  96. execp->a_trsize = ((obj_textsec (abfd)->reloc_count) * \
  97. obj_reloc_entry_size (abfd)); \
  98. execp->a_drsize = ((obj_datasec (abfd)->reloc_count) * \
  99. obj_reloc_entry_size (abfd)); \
  100. NAME (aout, swap_exec_header_out) (abfd, execp, & exec_bytes); \
  101. \
  102. if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0 \
  103. || bfd_bwrite ((void *) & exec_bytes, (bfd_size_type) EXEC_BYTES_SIZE, \
  104. abfd) != EXEC_BYTES_SIZE) \
  105. return FALSE; \
  106. /* Now write out reloc info, followed by syms and strings. */ \
  107. \
  108. if (bfd_get_outsymbols (abfd) != NULL \
  109. && bfd_get_symcount (abfd) != 0) \
  110. { \
  111. if (bfd_seek (abfd, (file_ptr) (N_SYMOFF (* execp)), SEEK_SET) != 0)\
  112. return FALSE; \
  113. \
  114. if (! NAME (aout, write_syms) (abfd)) \
  115. return FALSE; \
  116. \
  117. if (bfd_seek (abfd, (file_ptr) (N_TRELOFF (* execp)), SEEK_SET) != 0)\
  118. return FALSE; \
  119. \
  120. if (! riscix_squirt_out_relocs (abfd, obj_textsec (abfd))) \
  121. return FALSE; \
  122. if (bfd_seek (abfd, (file_ptr) (N_DRELOFF (* execp)), SEEK_SET) != 0)\
  123. return FALSE; \
  124. \
  125. if (!NAME (aout, squirt_out_relocs) (abfd, obj_datasec (abfd))) \
  126. return FALSE; \
  127. } \
  128. }
  129. #include "libaout.h"
  130. #include "aout/aout64.h"
  131. static bfd_reloc_status_type
  132. riscix_fix_pcrel_26_done (bfd *abfd ATTRIBUTE_UNUSED,
  133. arelent *reloc_entry ATTRIBUTE_UNUSED,
  134. asymbol *symbol ATTRIBUTE_UNUSED,
  135. void * data ATTRIBUTE_UNUSED,
  136. asection *input_section ATTRIBUTE_UNUSED,
  137. bfd *output_bfd ATTRIBUTE_UNUSED,
  138. char **error_message ATTRIBUTE_UNUSED)
  139. {
  140. /* This is dead simple at present. */
  141. return bfd_reloc_ok;
  142. }
  143. static bfd_reloc_status_type riscix_fix_pcrel_26 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
  144. static const bfd_target *arm_aout_riscix_callback (bfd *);
  145. static reloc_howto_type riscix_std_reloc_howto[] =
  146. {
  147. /* Type rs size bsz pcrel bitpos ovrf sf name part_inpl readmask setmask pcdone */
  148. HOWTO( 0, 0, 0, 8, FALSE, 0, complain_overflow_bitfield,0,"8", TRUE, 0x000000ff,0x000000ff, FALSE),
  149. HOWTO( 1, 0, 1, 16, FALSE, 0, complain_overflow_bitfield,0,"16", TRUE, 0x0000ffff,0x0000ffff, FALSE),
  150. HOWTO( 2, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,0,"32", TRUE, 0xffffffff,0xffffffff, FALSE),
  151. HOWTO( 3, 2, 3, 26, TRUE, 0, complain_overflow_signed, riscix_fix_pcrel_26 , "ARM26", TRUE, 0x00ffffff,0x00ffffff, FALSE),
  152. HOWTO( 4, 0, 0, 8, TRUE, 0, complain_overflow_signed, 0,"DISP8", TRUE, 0x000000ff,0x000000ff, TRUE),
  153. HOWTO( 5, 0, 1, 16, TRUE, 0, complain_overflow_signed, 0,"DISP16", TRUE, 0x0000ffff,0x0000ffff, TRUE),
  154. HOWTO( 6, 0, 2, 32, TRUE, 0, complain_overflow_signed, 0,"DISP32", TRUE, 0xffffffff,0xffffffff, TRUE),
  155. HOWTO( 7, 2, 3, 26, FALSE, 0, complain_overflow_signed, riscix_fix_pcrel_26_done, "ARM26D",TRUE,0x00ffffff,0x00ffffff, FALSE),
  156. EMPTY_HOWTO (-1),
  157. HOWTO( 9, 0, -1, 16, FALSE, 0, complain_overflow_bitfield,0,"NEG16", TRUE, 0x0000ffff,0x0000ffff, FALSE),
  158. HOWTO( 10, 0, -2, 32, FALSE, 0, complain_overflow_bitfield,0,"NEG32", TRUE, 0xffffffff,0xffffffff, FALSE)
  159. };
  160. #define RISCIX_TABLE_SIZE \
  161. (sizeof (riscix_std_reloc_howto) / sizeof (reloc_howto_type))
  162. static bfd_reloc_status_type
  163. riscix_fix_pcrel_26 (bfd *abfd,
  164. arelent *reloc_entry,
  165. asymbol *symbol,
  166. void * data,
  167. asection *input_section,
  168. bfd *output_bfd,
  169. char **error_message ATTRIBUTE_UNUSED)
  170. {
  171. bfd_vma relocation;
  172. bfd_size_type addr = reloc_entry->address;
  173. long target = bfd_get_32 (abfd, (bfd_byte *) data + addr);
  174. bfd_reloc_status_type flag = bfd_reloc_ok;
  175. /* If this is an undefined symbol, return error. */
  176. if (bfd_is_und_section (symbol->section)
  177. && (symbol->flags & BSF_WEAK) == 0)
  178. return output_bfd ? bfd_reloc_continue : bfd_reloc_undefined;
  179. /* If the sections are different, and we are doing a partial relocation,
  180. just ignore it for now. */
  181. if (symbol->section->name != input_section->name
  182. && output_bfd != NULL)
  183. return bfd_reloc_continue;
  184. relocation = (target & 0x00ffffff) << 2;
  185. relocation = (relocation ^ 0x02000000) - 0x02000000; /* Sign extend. */
  186. relocation += symbol->value;
  187. relocation += symbol->section->output_section->vma;
  188. relocation += symbol->section->output_offset;
  189. relocation += reloc_entry->addend;
  190. relocation -= input_section->output_section->vma;
  191. relocation -= input_section->output_offset;
  192. relocation -= addr;
  193. if (relocation & 3)
  194. return bfd_reloc_overflow;
  195. /* Check for overflow. */
  196. if (relocation & 0x02000000)
  197. {
  198. if ((relocation & ~ (bfd_vma) 0x03ffffff) != ~ (bfd_vma) 0x03ffffff)
  199. flag = bfd_reloc_overflow;
  200. }
  201. else if (relocation & ~ (bfd_vma) 0x03ffffff)
  202. flag = bfd_reloc_overflow;
  203. target &= ~0x00ffffff;
  204. target |= (relocation >> 2) & 0x00ffffff;
  205. bfd_put_32 (abfd, (bfd_vma) target, (bfd_byte *) data + addr);
  206. /* Now the ARM magic... Change the reloc type so that it is marked as done.
  207. Strictly this is only necessary if we are doing a partial relocation. */
  208. reloc_entry->howto = &riscix_std_reloc_howto[7];
  209. return flag;
  210. }
  211. static reloc_howto_type *
  212. riscix_reloc_type_lookup (bfd *abfd, bfd_reloc_code_real_type code)
  213. {
  214. #define ASTD(i,j) case i: return &riscix_std_reloc_howto[j]
  215. if (code == BFD_RELOC_CTOR)
  216. switch (bfd_arch_bits_per_address (abfd))
  217. {
  218. case 32:
  219. code = BFD_RELOC_32;
  220. break;
  221. default:
  222. return NULL;
  223. }
  224. switch (code)
  225. {
  226. ASTD (BFD_RELOC_16, 1);
  227. ASTD (BFD_RELOC_32, 2);
  228. ASTD (BFD_RELOC_ARM_PCREL_BRANCH, 3);
  229. ASTD (BFD_RELOC_8_PCREL, 4);
  230. ASTD (BFD_RELOC_16_PCREL, 5);
  231. ASTD (BFD_RELOC_32_PCREL, 6);
  232. default:
  233. return NULL;
  234. }
  235. }
  236. static reloc_howto_type *
  237. riscix_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
  238. const char *r_name)
  239. {
  240. unsigned int i;
  241. for (i = 0;
  242. i < sizeof (riscix_std_reloc_howto) / sizeof (riscix_std_reloc_howto[0]);
  243. i++)
  244. if (riscix_std_reloc_howto[i].name != NULL
  245. && strcasecmp (riscix_std_reloc_howto[i].name, r_name) == 0)
  246. return &riscix_std_reloc_howto[i];
  247. return NULL;
  248. }
  249. #define MY_bfd_link_hash_table_create _bfd_generic_link_hash_table_create
  250. #define MY_bfd_link_add_symbols _bfd_generic_link_add_symbols
  251. #define MY_final_link_callback should_not_be_used
  252. #define MY_bfd_final_link _bfd_generic_final_link
  253. #define MY_bfd_reloc_type_lookup riscix_reloc_type_lookup
  254. #define MY_bfd_reloc_name_lookup riscix_reloc_name_lookup
  255. #define MY_canonicalize_reloc arm_aout_riscix_canonicalize_reloc
  256. #define MY_object_p arm_aout_riscix_object_p
  257. static void
  258. riscix_swap_std_reloc_out (bfd *abfd,
  259. arelent *g,
  260. struct reloc_std_external *natptr)
  261. {
  262. int r_index;
  263. asymbol *sym = *(g->sym_ptr_ptr);
  264. int r_extern;
  265. int r_length;
  266. int r_pcrel;
  267. int r_neg = 0; /* Negative relocs use the BASEREL bit. */
  268. asection *output_section = sym->section->output_section;
  269. PUT_WORD(abfd, g->address, natptr->r_address);
  270. r_length = g->howto->size ; /* Size as a power of two. */
  271. if (r_length < 0)
  272. {
  273. r_length = -r_length;
  274. r_neg = 1;
  275. }
  276. r_pcrel = (int) g->howto->pc_relative; /* Relative to PC? */
  277. /* For RISC iX, in pc-relative relocs the r_pcrel bit means that the
  278. relocation has been done already (Only for the 26-bit one I think)? */
  279. if (r_length == 3)
  280. r_pcrel = r_pcrel ? 0 : 1;
  281. /* Name was clobbered by aout_write_syms to be symbol index. */
  282. /* If this relocation is relative to a symbol then set the
  283. r_index to the symbols index, and the r_extern bit.
  284. Absolute symbols can come in in two ways, either as an offset
  285. from the abs section, or as a symbol which has an abs value.
  286. check for that here. */
  287. if (bfd_is_com_section (output_section)
  288. || bfd_is_abs_section (output_section)
  289. || bfd_is_und_section (output_section))
  290. {
  291. if (bfd_abs_section_ptr->symbol == sym)
  292. {
  293. /* Whoops, looked like an abs symbol, but is really an offset
  294. from the abs section. */
  295. r_index = 0;
  296. r_extern = 0;
  297. }
  298. else
  299. {
  300. /* Fill in symbol. */
  301. r_extern = 1;
  302. r_index = (*g->sym_ptr_ptr)->udata.i;
  303. }
  304. }
  305. else
  306. {
  307. /* Just an ordinary section. */
  308. r_extern = 0;
  309. r_index = output_section->target_index;
  310. }
  311. /* Now the fun stuff. */
  312. if (bfd_header_big_endian (abfd))
  313. {
  314. natptr->r_index[0] = r_index >> 16;
  315. natptr->r_index[1] = r_index >> 8;
  316. natptr->r_index[2] = r_index;
  317. natptr->r_type[0] =
  318. ( (r_extern ? RELOC_STD_BITS_EXTERN_BIG: 0)
  319. | (r_pcrel ? RELOC_STD_BITS_PCREL_BIG: 0)
  320. | (r_neg ? RELOC_STD_BITS_BASEREL_BIG: 0)
  321. | (r_length << RELOC_STD_BITS_LENGTH_SH_BIG));
  322. }
  323. else
  324. {
  325. natptr->r_index[2] = r_index >> 16;
  326. natptr->r_index[1] = r_index >> 8;
  327. natptr->r_index[0] = r_index;
  328. natptr->r_type[0] =
  329. ( (r_extern ? RELOC_STD_BITS_EXTERN_LITTLE: 0)
  330. | (r_pcrel ? RELOC_STD_BITS_PCREL_LITTLE: 0)
  331. | (r_neg ? RELOC_STD_BITS_BASEREL_LITTLE: 0)
  332. | (r_length << RELOC_STD_BITS_LENGTH_SH_LITTLE));
  333. }
  334. }
  335. static bfd_boolean
  336. riscix_squirt_out_relocs (bfd *abfd, asection *section)
  337. {
  338. arelent **generic;
  339. unsigned char *native, *natptr;
  340. size_t each_size;
  341. unsigned int count = section->reloc_count;
  342. bfd_size_type natsize;
  343. if (count == 0)
  344. return TRUE;
  345. each_size = obj_reloc_entry_size (abfd);
  346. natsize = each_size;
  347. natsize *= count;
  348. native = bfd_zalloc (abfd, natsize);
  349. if (!native)
  350. return FALSE;
  351. generic = section->orelocation;
  352. for (natptr = native;
  353. count != 0;
  354. --count, natptr += each_size, ++generic)
  355. riscix_swap_std_reloc_out (abfd, *generic,
  356. (struct reloc_std_external *) natptr);
  357. if (bfd_bwrite ((void *) native, natsize, abfd) != natsize)
  358. {
  359. bfd_release (abfd, native);
  360. return FALSE;
  361. }
  362. bfd_release (abfd, native);
  363. return TRUE;
  364. }
  365. /* This is just like the standard aoutx.h version but we need to do our
  366. own mapping of external reloc type values to howto entries. */
  367. static long
  368. MY (canonicalize_reloc) (bfd *abfd,
  369. sec_ptr section,
  370. arelent **relptr,
  371. asymbol **symbols)
  372. {
  373. arelent *tblptr = section->relocation;
  374. unsigned int count, c;
  375. extern reloc_howto_type NAME (aout, std_howto_table)[];
  376. /* If we have already read in the relocation table, return the values. */
  377. if (section->flags & SEC_CONSTRUCTOR)
  378. {
  379. arelent_chain *chain = section->constructor_chain;
  380. for (count = 0; count < section->reloc_count; count++)
  381. {
  382. *relptr++ = &chain->relent;
  383. chain = chain->next;
  384. }
  385. *relptr = 0;
  386. return section->reloc_count;
  387. }
  388. if (tblptr && section->reloc_count)
  389. {
  390. for (count = 0; count++ < section->reloc_count;)
  391. *relptr++ = tblptr++;
  392. *relptr = 0;
  393. return section->reloc_count;
  394. }
  395. if (!NAME (aout, slurp_reloc_table) (abfd, section, symbols))
  396. return -1;
  397. tblptr = section->relocation;
  398. /* Fix up howto entries. */
  399. for (count = 0; count++ < section->reloc_count;)
  400. {
  401. c = tblptr->howto - NAME(aout,std_howto_table);
  402. BFD_ASSERT (c < RISCIX_TABLE_SIZE);
  403. tblptr->howto = &riscix_std_reloc_howto[c];
  404. *relptr++ = tblptr++;
  405. }
  406. *relptr = 0;
  407. return section->reloc_count;
  408. }
  409. /* This is the same as NAME(aout,some_aout_object_p), but has different
  410. expansions of the macro definitions. */
  411. static const bfd_target *
  412. riscix_some_aout_object_p (bfd *abfd,
  413. struct internal_exec *execp,
  414. const bfd_target *(*callback_to_real_object_p) (bfd *))
  415. {
  416. struct aout_data_struct *rawptr, *oldrawptr;
  417. const bfd_target *result;
  418. bfd_size_type amt = sizeof (struct aout_data_struct);
  419. rawptr = bfd_zalloc (abfd, amt);
  420. if (rawptr == NULL)
  421. return NULL;
  422. oldrawptr = abfd->tdata.aout_data;
  423. abfd->tdata.aout_data = rawptr;
  424. /* Copy the contents of the old tdata struct.
  425. In particular, we want the subformat, since for hpux it was set in
  426. hp300hpux.c:swap_exec_header_in and will be used in
  427. hp300hpux.c:callback. */
  428. if (oldrawptr != NULL)
  429. *abfd->tdata.aout_data = *oldrawptr;
  430. abfd->tdata.aout_data->a.hdr = &rawptr->e;
  431. /* Copy in the internal_exec struct. */
  432. *(abfd->tdata.aout_data->a.hdr) = *execp;
  433. execp = abfd->tdata.aout_data->a.hdr;
  434. /* Set the file flags. */
  435. abfd->flags = BFD_NO_FLAGS;
  436. if (execp->a_drsize || execp->a_trsize)
  437. abfd->flags |= HAS_RELOC;
  438. /* Setting of EXEC_P has been deferred to the bottom of this function. */
  439. if (execp->a_syms)
  440. abfd->flags |= HAS_LINENO | HAS_DEBUG | HAS_SYMS | HAS_LOCALS;
  441. if (N_DYNAMIC(*execp))
  442. abfd->flags |= DYNAMIC;
  443. /* Squeezed files aren't supported (yet)! */
  444. if ((execp->a_info & MF_SQUEEZED) != 0)
  445. {
  446. bfd_set_error (bfd_error_wrong_format);
  447. return NULL;
  448. }
  449. else if ((execp->a_info & MF_IS_SL) != 0)
  450. {
  451. /* Nor are shared libraries. */
  452. bfd_set_error (bfd_error_wrong_format);
  453. return NULL;
  454. }
  455. else if (N_MAGIC (*execp) == ZMAGIC)
  456. {
  457. abfd->flags |= D_PAGED | WP_TEXT;
  458. adata (abfd).magic = z_magic;
  459. }
  460. else if (N_MAGIC (*execp) == NMAGIC)
  461. {
  462. abfd->flags |= WP_TEXT;
  463. adata (abfd).magic = n_magic;
  464. }
  465. else if (N_MAGIC (*execp) == OMAGIC)
  466. adata (abfd).magic = o_magic;
  467. else
  468. /* Should have been checked with N_BADMAG before this routine
  469. was called. */
  470. abort ();
  471. bfd_get_start_address (abfd) = execp->a_entry;
  472. obj_aout_symbols (abfd) = NULL;
  473. bfd_get_symcount (abfd) = execp->a_syms / sizeof (struct external_nlist);
  474. /* The default relocation entry size is that of traditional V7 Unix. */
  475. obj_reloc_entry_size (abfd) = RELOC_STD_SIZE;
  476. /* The default symbol entry size is that of traditional Unix. */
  477. obj_symbol_entry_size (abfd) = EXTERNAL_NLIST_SIZE;
  478. obj_aout_external_syms (abfd) = NULL;
  479. obj_aout_external_strings (abfd) = NULL;
  480. obj_aout_sym_hashes (abfd) = NULL;
  481. if (! NAME (aout, make_sections) (abfd))
  482. return NULL;
  483. obj_datasec (abfd)->size = execp->a_data;
  484. obj_bsssec (abfd)->size = execp->a_bss;
  485. obj_textsec (abfd)->flags =
  486. (execp->a_trsize != 0
  487. ? (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_HAS_CONTENTS | SEC_RELOC)
  488. : (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_HAS_CONTENTS));
  489. obj_datasec (abfd)->flags =
  490. (execp->a_drsize != 0
  491. ? (SEC_ALLOC | SEC_LOAD | SEC_DATA | SEC_HAS_CONTENTS | SEC_RELOC)
  492. : (SEC_ALLOC | SEC_LOAD | SEC_DATA | SEC_HAS_CONTENTS));
  493. obj_bsssec (abfd)->flags = SEC_ALLOC;
  494. result = (*callback_to_real_object_p) (abfd);
  495. #if defined(MACH) || defined(STAT_FOR_EXEC)
  496. /* The original heuristic doesn't work in some important cases. The
  497. a.out file has no information about the text start address. For
  498. files (like kernels) linked to non-standard addresses (ld -Ttext
  499. nnn) the entry point may not be between the default text start
  500. (obj_textsec(abfd)->vma) and (obj_textsec(abfd)->vma) + text size
  501. This is not just a mach issue. Many kernels are loaded at non
  502. standard addresses. */
  503. {
  504. struct stat stat_buf;
  505. if (abfd->iostream != NULL
  506. && (abfd->flags & BFD_IN_MEMORY) == 0
  507. && (fstat(fileno((FILE *) (abfd->iostream)), &stat_buf) == 0)
  508. && ((stat_buf.st_mode & 0111) != 0))
  509. abfd->flags |= EXEC_P;
  510. }
  511. #else /* ! MACH */
  512. /* Now that the segment addresses have been worked out, take a better
  513. guess at whether the file is executable. If the entry point
  514. is within the text segment, assume it is. (This makes files
  515. executable even if their entry point address is 0, as long as
  516. their text starts at zero.)
  517. At some point we should probably break down and stat the file and
  518. declare it executable if (one of) its 'x' bits are on... */
  519. if ((execp->a_entry >= obj_textsec(abfd)->vma) &&
  520. (execp->a_entry < obj_textsec(abfd)->vma + obj_textsec(abfd)->size))
  521. abfd->flags |= EXEC_P;
  522. #endif /* MACH */
  523. if (result == NULL)
  524. {
  525. free (rawptr);
  526. abfd->tdata.aout_data = oldrawptr;
  527. }
  528. return result;
  529. }
  530. static const bfd_target *
  531. MY (object_p) (bfd *abfd)
  532. {
  533. struct external_exec exec_bytes; /* Raw exec header from file. */
  534. struct internal_exec exec; /* Cleaned-up exec header. */
  535. const bfd_target *target;
  536. if (bfd_bread ((void *) &exec_bytes, (bfd_size_type) EXEC_BYTES_SIZE, abfd)
  537. != EXEC_BYTES_SIZE)
  538. {
  539. if (bfd_get_error () != bfd_error_system_call)
  540. bfd_set_error (bfd_error_wrong_format);
  541. return NULL;
  542. }
  543. exec.a_info = H_GET_32 (abfd, exec_bytes.e_info);
  544. if (N_BADMAG (exec))
  545. return NULL;
  546. #ifdef MACHTYPE_OK
  547. if (!(MACHTYPE_OK (N_MACHTYPE (exec))))
  548. return NULL;
  549. #endif
  550. NAME (aout, swap_exec_header_in) (abfd, & exec_bytes, & exec);
  551. target = riscix_some_aout_object_p (abfd, & exec, MY (callback));
  552. return target;
  553. }
  554. #include "aout-target.h"