i386lynx.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  1. /* BFD back-end for i386 a.out binaries under LynxOS.
  2. Copyright (C) 1990-2022 Free Software Foundation, Inc.
  3. This file is part of BFD, the Binary File Descriptor library.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
  15. MA 02110-1301, USA. */
  16. #define TEXT_START_ADDR 0
  17. #define TARGET_PAGE_SIZE 4096
  18. #define SEGMENT_SIZE TARGET_PAGE_SIZE
  19. #define DEFAULT_ARCH bfd_arch_i386
  20. /* Do not "beautify" the CONCAT* macro args. Traditional C will not
  21. remove whitespace added here, and thus will fail to concatenate
  22. the tokens. */
  23. #define MY(OP) CONCAT2 (i386_aout_lynx_,OP)
  24. #define TARGETNAME "a.out-i386-lynx"
  25. #include "sysdep.h"
  26. #include "bfd.h"
  27. #include "libbfd.h"
  28. #ifndef WRITE_HEADERS
  29. #define WRITE_HEADERS(abfd, execp) \
  30. { \
  31. if (adata(abfd).magic == undecided_magic) \
  32. NAME(aout,adjust_sizes_and_vmas) (abfd); \
  33. \
  34. execp->a_syms = bfd_get_symcount (abfd) * EXTERNAL_NLIST_SIZE; \
  35. execp->a_entry = bfd_get_start_address (abfd); \
  36. \
  37. execp->a_trsize = ((obj_textsec (abfd)->reloc_count) * \
  38. obj_reloc_entry_size (abfd)); \
  39. execp->a_drsize = ((obj_datasec (abfd)->reloc_count) * \
  40. obj_reloc_entry_size (abfd)); \
  41. NAME(aout,swap_exec_header_out) (abfd, execp, &exec_bytes); \
  42. \
  43. if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0 \
  44. || bfd_bwrite (&exec_bytes, (bfd_size_type) EXEC_BYTES_SIZE, \
  45. abfd) != EXEC_BYTES_SIZE) \
  46. return false; \
  47. /* Now write out reloc info, followed by syms and strings */ \
  48. \
  49. if (bfd_get_symcount (abfd) != 0) \
  50. { \
  51. if (bfd_seek (abfd, (file_ptr) (N_SYMOFF (execp)), SEEK_SET) \
  52. != 0) \
  53. return false; \
  54. \
  55. if (! NAME(aout,write_syms) (abfd)) return false; \
  56. \
  57. if (bfd_seek (abfd, (file_ptr) (N_TRELOFF (execp)), SEEK_SET) \
  58. != 0) \
  59. return false; \
  60. \
  61. if (!NAME(lynx,squirt_out_relocs) (abfd, obj_textsec (abfd))) \
  62. return false; \
  63. if (bfd_seek (abfd, (file_ptr) (N_DRELOFF (execp)), SEEK_SET) \
  64. != 0) \
  65. return 0; \
  66. \
  67. if (!NAME(lynx,squirt_out_relocs) (abfd, obj_datasec (abfd))) \
  68. return false; \
  69. } \
  70. }
  71. #endif
  72. #include "libaout.h"
  73. #include "aout/aout64.h"
  74. #ifdef LYNX_CORE
  75. char *lynx_core_file_failing_command ();
  76. int lynx_core_file_failing_signal ();
  77. bool lynx_core_file_matches_executable_p ();
  78. const bfd_target *lynx_core_file_p ();
  79. #define MY_core_file_failing_command lynx_core_file_failing_command
  80. #define MY_core_file_failing_signal lynx_core_file_failing_signal
  81. #define MY_core_file_matches_executable_p lynx_core_file_matches_executable_p
  82. #define MY_core_file_p lynx_core_file_p
  83. #endif /* LYNX_CORE */
  84. #define KEEPIT udata.i
  85. extern reloc_howto_type aout_32_ext_howto_table[];
  86. extern reloc_howto_type aout_32_std_howto_table[];
  87. /* Standard reloc stuff */
  88. /* Output standard relocation information to a file in target byte order. */
  89. static void
  90. NAME(lynx,swap_std_reloc_out) (bfd *abfd,
  91. arelent *g,
  92. struct reloc_std_external *natptr)
  93. {
  94. int r_index;
  95. asymbol *sym = *(g->sym_ptr_ptr);
  96. int r_extern;
  97. unsigned int r_length;
  98. int r_pcrel;
  99. int r_baserel, r_jmptable, r_relative;
  100. asection *output_section = sym->section->output_section;
  101. PUT_WORD (abfd, g->address, natptr->r_address);
  102. r_length = g->howto->size; /* Size as a power of two */
  103. r_pcrel = (int) g->howto->pc_relative; /* Relative to PC? */
  104. /* r_baserel, r_jmptable, r_relative??? FIXME-soon */
  105. r_baserel = 0;
  106. r_jmptable = 0;
  107. r_relative = 0;
  108. /* name was clobbered by aout_write_syms to be symbol index */
  109. /* If this relocation is relative to a symbol then set the
  110. r_index to the symbols index, and the r_extern bit.
  111. Absolute symbols can come in in two ways, either as an offset
  112. from the abs section, or as a symbol which has an abs value.
  113. check for that here
  114. */
  115. if (bfd_is_com_section (output_section)
  116. || bfd_is_abs_section (output_section)
  117. || bfd_is_und_section (output_section))
  118. {
  119. if (bfd_abs_section_ptr->symbol == sym)
  120. {
  121. /* Whoops, looked like an abs symbol, but is really an offset
  122. from the abs section */
  123. r_index = 0;
  124. r_extern = 0;
  125. }
  126. else
  127. {
  128. /* Fill in symbol */
  129. r_extern = 1;
  130. r_index = (*g->sym_ptr_ptr)->KEEPIT;
  131. }
  132. }
  133. else
  134. {
  135. /* Just an ordinary section */
  136. r_extern = 0;
  137. r_index = output_section->target_index;
  138. }
  139. /* now the fun stuff */
  140. if (bfd_header_big_endian (abfd))
  141. {
  142. natptr->r_index[0] = r_index >> 16;
  143. natptr->r_index[1] = r_index >> 8;
  144. natptr->r_index[2] = r_index;
  145. natptr->r_type[0] =
  146. (r_extern ? RELOC_STD_BITS_EXTERN_BIG : 0)
  147. | (r_pcrel ? RELOC_STD_BITS_PCREL_BIG : 0)
  148. | (r_baserel ? RELOC_STD_BITS_BASEREL_BIG : 0)
  149. | (r_jmptable ? RELOC_STD_BITS_JMPTABLE_BIG : 0)
  150. | (r_relative ? RELOC_STD_BITS_RELATIVE_BIG : 0)
  151. | (r_length << RELOC_STD_BITS_LENGTH_SH_BIG);
  152. }
  153. else
  154. {
  155. natptr->r_index[2] = r_index >> 16;
  156. natptr->r_index[1] = r_index >> 8;
  157. natptr->r_index[0] = r_index;
  158. natptr->r_type[0] =
  159. (r_extern ? RELOC_STD_BITS_EXTERN_LITTLE : 0)
  160. | (r_pcrel ? RELOC_STD_BITS_PCREL_LITTLE : 0)
  161. | (r_baserel ? RELOC_STD_BITS_BASEREL_LITTLE : 0)
  162. | (r_jmptable ? RELOC_STD_BITS_JMPTABLE_LITTLE : 0)
  163. | (r_relative ? RELOC_STD_BITS_RELATIVE_LITTLE : 0)
  164. | (r_length << RELOC_STD_BITS_LENGTH_SH_LITTLE);
  165. }
  166. }
  167. /* Extended stuff */
  168. /* Output extended relocation information to a file in target byte order. */
  169. static void
  170. NAME(lynx,swap_ext_reloc_out) (bfd *abfd,
  171. arelent *g,
  172. struct reloc_ext_external *natptr)
  173. {
  174. int r_index;
  175. int r_extern;
  176. unsigned int r_type;
  177. unsigned int r_addend;
  178. asymbol *sym = *(g->sym_ptr_ptr);
  179. asection *output_section = sym->section->output_section;
  180. PUT_WORD (abfd, g->address, natptr->r_address);
  181. r_type = (unsigned int) g->howto->type;
  182. r_addend = g->addend + (*(g->sym_ptr_ptr))->section->output_section->vma;
  183. /* If this relocation is relative to a symbol then set the
  184. r_index to the symbols index, and the r_extern bit.
  185. Absolute symbols can come in in two ways, either as an offset
  186. from the abs section, or as a symbol which has an abs value.
  187. check for that here
  188. */
  189. if (bfd_is_com_section (output_section)
  190. || bfd_is_abs_section (output_section)
  191. || bfd_is_und_section (output_section))
  192. {
  193. if (bfd_abs_section_ptr->symbol == sym)
  194. {
  195. /* Whoops, looked like an abs symbol, but is really an offset
  196. from the abs section */
  197. r_index = 0;
  198. r_extern = 0;
  199. }
  200. else
  201. {
  202. r_extern = 1;
  203. r_index = (*g->sym_ptr_ptr)->KEEPIT;
  204. }
  205. }
  206. else
  207. {
  208. /* Just an ordinary section */
  209. r_extern = 0;
  210. r_index = output_section->target_index;
  211. }
  212. /* now the fun stuff */
  213. if (bfd_header_big_endian (abfd))
  214. {
  215. natptr->r_index[0] = r_index >> 16;
  216. natptr->r_index[1] = r_index >> 8;
  217. natptr->r_index[2] = r_index;
  218. natptr->r_type[0] =
  219. (r_extern ? RELOC_EXT_BITS_EXTERN_BIG : 0)
  220. | (r_type << RELOC_EXT_BITS_TYPE_SH_BIG);
  221. }
  222. else
  223. {
  224. natptr->r_index[2] = r_index >> 16;
  225. natptr->r_index[1] = r_index >> 8;
  226. natptr->r_index[0] = r_index;
  227. natptr->r_type[0] =
  228. (r_extern ? RELOC_EXT_BITS_EXTERN_LITTLE : 0)
  229. | (r_type << RELOC_EXT_BITS_TYPE_SH_LITTLE);
  230. }
  231. PUT_WORD (abfd, r_addend, natptr->r_addend);
  232. }
  233. /* BFD deals internally with all things based from the section they're
  234. in. so, something in 10 bytes into a text section with a base of
  235. 50 would have a symbol (.text+10) and know .text vma was 50.
  236. Aout keeps all it's symbols based from zero, so the symbol would
  237. contain 60. This macro subs the base of each section from the value
  238. to give the true offset from the section */
  239. #define MOVE_ADDRESS(ad) \
  240. if (r_extern) \
  241. { \
  242. /* undefined symbol */ \
  243. if (r_index < bfd_get_symcount (abfd)) \
  244. cache_ptr->sym_ptr_ptr = symbols + r_index; \
  245. cache_ptr->addend = ad; \
  246. } \
  247. else \
  248. { \
  249. /* defined, section relative. replace symbol with pointer to \
  250. symbol which points to section */ \
  251. switch (r_index) \
  252. { \
  253. case N_TEXT: \
  254. case N_TEXT | N_EXT: \
  255. cache_ptr->sym_ptr_ptr = obj_textsec(abfd)->symbol_ptr_ptr; \
  256. cache_ptr->addend = ad - su->textsec->vma; \
  257. break; \
  258. case N_DATA: \
  259. case N_DATA | N_EXT: \
  260. cache_ptr->sym_ptr_ptr = obj_datasec(abfd)->symbol_ptr_ptr; \
  261. cache_ptr->addend = ad - su->datasec->vma; \
  262. break; \
  263. case N_BSS: \
  264. case N_BSS | N_EXT: \
  265. cache_ptr->sym_ptr_ptr = obj_bsssec(abfd)->symbol_ptr_ptr; \
  266. cache_ptr->addend = ad - su->bsssec->vma; \
  267. break; \
  268. default: \
  269. case N_ABS: \
  270. case N_ABS | N_EXT: \
  271. cache_ptr->sym_ptr_ptr = bfd_abs_section_ptr->symbol_ptr_ptr; \
  272. cache_ptr->addend = ad; \
  273. break; \
  274. } \
  275. } \
  276. static void
  277. NAME(lynx,swap_ext_reloc_in) (bfd *abfd,
  278. struct reloc_ext_external *bytes,
  279. arelent *cache_ptr,
  280. asymbol **symbols,
  281. bfd_size_type symcount ATTRIBUTE_UNUSED)
  282. {
  283. unsigned int r_index;
  284. int r_extern;
  285. unsigned int r_type;
  286. struct aoutdata *su = &(abfd->tdata.aout_data->a);
  287. cache_ptr->address = (GET_SWORD (abfd, bytes->r_address));
  288. r_index = bytes->r_index[1];
  289. r_extern = (0 != (bytes->r_index[0] & RELOC_EXT_BITS_EXTERN_BIG));
  290. r_type = (bytes->r_index[0] & RELOC_EXT_BITS_TYPE_BIG)
  291. >> RELOC_EXT_BITS_TYPE_SH_BIG;
  292. cache_ptr->howto = aout_32_ext_howto_table + r_type;
  293. MOVE_ADDRESS (GET_SWORD (abfd, bytes->r_addend));
  294. }
  295. static void
  296. NAME(lynx,swap_std_reloc_in) (bfd *abfd,
  297. struct reloc_std_external *bytes,
  298. arelent *cache_ptr,
  299. asymbol **symbols,
  300. bfd_size_type symcount ATTRIBUTE_UNUSED)
  301. {
  302. unsigned int r_index;
  303. int r_extern;
  304. unsigned int r_length;
  305. int r_pcrel;
  306. struct aoutdata *su = &(abfd->tdata.aout_data->a);
  307. cache_ptr->address = H_GET_32 (abfd, bytes->r_address);
  308. r_index = bytes->r_index[1];
  309. r_extern = (0 != (bytes->r_index[0] & RELOC_STD_BITS_EXTERN_BIG));
  310. r_pcrel = (0 != (bytes->r_index[0] & RELOC_STD_BITS_PCREL_BIG));
  311. r_length = (bytes->r_index[0] & RELOC_STD_BITS_LENGTH_BIG)
  312. >> RELOC_STD_BITS_LENGTH_SH_BIG;
  313. cache_ptr->howto = aout_32_std_howto_table + r_length + 4 * r_pcrel;
  314. /* FIXME-soon: Roll baserel, jmptable, relative bits into howto setting */
  315. MOVE_ADDRESS (0);
  316. }
  317. /* Reloc hackery */
  318. static bool
  319. NAME(lynx,slurp_reloc_table) (bfd *abfd,
  320. sec_ptr asect,
  321. asymbol **symbols)
  322. {
  323. bfd_size_type count;
  324. bfd_size_type reloc_size;
  325. void * relocs;
  326. arelent *reloc_cache;
  327. size_t each_size;
  328. if (asect->relocation)
  329. return true;
  330. if (asect->flags & SEC_CONSTRUCTOR)
  331. return true;
  332. if (asect == obj_datasec (abfd))
  333. {
  334. reloc_size = exec_hdr (abfd)->a_drsize;
  335. goto doit;
  336. }
  337. if (asect == obj_textsec (abfd))
  338. {
  339. reloc_size = exec_hdr (abfd)->a_trsize;
  340. goto doit;
  341. }
  342. bfd_set_error (bfd_error_invalid_operation);
  343. return false;
  344. doit:
  345. if (bfd_seek (abfd, asect->rel_filepos, SEEK_SET) != 0)
  346. return false;
  347. each_size = obj_reloc_entry_size (abfd);
  348. count = reloc_size / each_size;
  349. reloc_cache = (arelent *) bfd_zmalloc (count * sizeof (arelent));
  350. if (!reloc_cache && count != 0)
  351. return false;
  352. relocs = _bfd_alloc_and_read (abfd, reloc_size, reloc_size);
  353. if (!relocs && reloc_size != 0)
  354. {
  355. free (reloc_cache);
  356. return false;
  357. }
  358. if (each_size == RELOC_EXT_SIZE)
  359. {
  360. struct reloc_ext_external *rptr = (struct reloc_ext_external *) relocs;
  361. unsigned int counter = 0;
  362. arelent *cache_ptr = reloc_cache;
  363. for (; counter < count; counter++, rptr++, cache_ptr++)
  364. {
  365. NAME(lynx,swap_ext_reloc_in) (abfd, rptr, cache_ptr, symbols,
  366. (bfd_size_type) bfd_get_symcount (abfd));
  367. }
  368. }
  369. else
  370. {
  371. struct reloc_std_external *rptr = (struct reloc_std_external *) relocs;
  372. unsigned int counter = 0;
  373. arelent *cache_ptr = reloc_cache;
  374. for (; counter < count; counter++, rptr++, cache_ptr++)
  375. {
  376. NAME(lynx,swap_std_reloc_in) (abfd, rptr, cache_ptr, symbols,
  377. (bfd_size_type) bfd_get_symcount (abfd));
  378. }
  379. }
  380. bfd_release (abfd, relocs);
  381. asect->relocation = reloc_cache;
  382. asect->reloc_count = count;
  383. return true;
  384. }
  385. /* Write out a relocation section into an object file. */
  386. static bool
  387. NAME(lynx,squirt_out_relocs) (bfd *abfd, asection *section)
  388. {
  389. arelent **generic;
  390. unsigned char *native, *natptr;
  391. size_t each_size;
  392. unsigned int count = section->reloc_count;
  393. bfd_size_type natsize;
  394. if (count == 0)
  395. return true;
  396. each_size = obj_reloc_entry_size (abfd);
  397. natsize = count;
  398. natsize *= each_size;
  399. native = (unsigned char *) bfd_zalloc (abfd, natsize);
  400. if (!native)
  401. return false;
  402. generic = section->orelocation;
  403. if (each_size == RELOC_EXT_SIZE)
  404. {
  405. for (natptr = native;
  406. count != 0;
  407. --count, natptr += each_size, ++generic)
  408. NAME(lynx,swap_ext_reloc_out) (abfd, *generic, (struct reloc_ext_external *) natptr);
  409. }
  410. else
  411. {
  412. for (natptr = native;
  413. count != 0;
  414. --count, natptr += each_size, ++generic)
  415. NAME(lynx,swap_std_reloc_out) (abfd, *generic, (struct reloc_std_external *) natptr);
  416. }
  417. if (bfd_bwrite (native, natsize, abfd) != natsize)
  418. {
  419. bfd_release (abfd, native);
  420. return false;
  421. }
  422. bfd_release (abfd, native);
  423. return true;
  424. }
  425. /* This is stupid. This function should be a boolean predicate */
  426. static long
  427. NAME(lynx,canonicalize_reloc) (bfd *abfd,
  428. sec_ptr section,
  429. arelent **relptr,
  430. asymbol **symbols)
  431. {
  432. arelent *tblptr = section->relocation;
  433. unsigned int count;
  434. if (!(tblptr || NAME(lynx,slurp_reloc_table) (abfd, section, symbols)))
  435. return -1;
  436. if (section->flags & SEC_CONSTRUCTOR)
  437. {
  438. arelent_chain *chain = section->constructor_chain;
  439. for (count = 0; count < section->reloc_count; count++)
  440. {
  441. *relptr++ = &chain->relent;
  442. chain = chain->next;
  443. }
  444. }
  445. else
  446. {
  447. tblptr = section->relocation;
  448. for (count = 0; count++ < section->reloc_count;)
  449. {
  450. *relptr++ = tblptr++;
  451. }
  452. }
  453. *relptr = 0;
  454. return section->reloc_count;
  455. }
  456. #define MY_canonicalize_reloc NAME(lynx,canonicalize_reloc)
  457. #include "aout-target.h"