nlm32-alpha.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860
  1. /* Support for 32-bit Alpha NLM (NetWare Loadable Module)
  2. Copyright (C) 1993-2015 Free Software Foundation, Inc.
  3. Written by Ian Lance Taylor, Cygnus Support.
  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. /* This file describes the 32 bit Alpha NLM format. You might think
  18. that an Alpha chip would use a 64 bit format, but, for some reason,
  19. it doesn't. */
  20. #include "sysdep.h"
  21. #include "bfd.h"
  22. #include "libbfd.h"
  23. #define ARCH_SIZE 32
  24. #include "nlm/alpha-ext.h"
  25. #define Nlm_External_Fixed_Header Nlm32_alpha_External_Fixed_Header
  26. #include "libnlm.h"
  27. /* Alpha NLM's have a prefix header before the standard NLM. This
  28. function reads it in, verifies the version, and seeks the bfd to
  29. the location before the regular NLM header. */
  30. static bfd_boolean
  31. nlm_alpha_backend_object_p (bfd *abfd)
  32. {
  33. struct nlm32_alpha_external_prefix_header s;
  34. file_ptr size;
  35. if (bfd_bread (&s, (bfd_size_type) sizeof s, abfd) != sizeof s)
  36. return FALSE;
  37. if (H_GET_32 (abfd, s.magic) != NLM32_ALPHA_MAGIC)
  38. return FALSE;
  39. /* FIXME: Should we check the format number? */
  40. /* Skip to the end of the header. */
  41. size = H_GET_32 (abfd, s.size);
  42. if (bfd_seek (abfd, size, SEEK_SET) != 0)
  43. return FALSE;
  44. return TRUE;
  45. }
  46. /* Write out the prefix. */
  47. static bfd_boolean
  48. nlm_alpha_write_prefix (bfd *abfd)
  49. {
  50. struct nlm32_alpha_external_prefix_header s;
  51. memset (&s, 0, sizeof s);
  52. H_PUT_32 (abfd, NLM32_ALPHA_MAGIC, s.magic);
  53. H_PUT_32 (abfd, 2, s.format);
  54. H_PUT_32 (abfd, sizeof s, s.size);
  55. if (bfd_bwrite (&s, (bfd_size_type) sizeof s, abfd) != sizeof s)
  56. return FALSE;
  57. return TRUE;
  58. }
  59. #define ONES(n) (((bfd_vma) 1 << ((n) - 1) << 1) - 1)
  60. /* How to process the various reloc types. */
  61. static reloc_howto_type nlm32_alpha_howto_table[] =
  62. {
  63. /* Reloc type 0 is ignored by itself. However, it appears after a
  64. GPDISP reloc to identify the location where the low order 16 bits
  65. of the gp register are loaded. */
  66. HOWTO (ALPHA_R_IGNORE, /* Type. */
  67. 0, /* Rightshift. */
  68. 0, /* Size (0 = byte, 1 = short, 2 = long). */
  69. 8, /* Bitsize. */
  70. FALSE, /* PC_relative. */
  71. 0, /* Bitpos. */
  72. complain_overflow_dont, /* Complain_on_overflow. */
  73. 0, /* Special_function. */
  74. "IGNORE", /* Name. */
  75. FALSE, /* Partial_inplace. */
  76. 0, /* Source mask. */
  77. 0, /* Dest mask. */
  78. FALSE), /* PCrel_offset. */
  79. /* A 32 bit reference to a symbol. */
  80. HOWTO (ALPHA_R_REFLONG, /* Type. */
  81. 0, /* Rightshift. */
  82. 2, /* Size (0 = byte, 1 = short, 2 = long). */
  83. 32, /* Bitsize. */
  84. FALSE, /* PC_relative. */
  85. 0, /* Bitpos. */
  86. complain_overflow_bitfield, /* Complain_on_overflow. */
  87. 0, /* Special_function. */
  88. "REFLONG", /* Name. */
  89. TRUE, /* Partial_inplace. */
  90. 0xffffffff, /* Source mask. */
  91. 0xffffffff, /* Dest mask. */
  92. FALSE), /* PCrel_offset. */
  93. /* A 64 bit reference to a symbol. */
  94. HOWTO (ALPHA_R_REFQUAD, /* Type. */
  95. 0, /* Rightshift. */
  96. 4, /* Size (0 = byte, 1 = short, 2 = long). */
  97. 64, /* Bitsize. */
  98. FALSE, /* PC_relative. */
  99. 0, /* Bitpos. */
  100. complain_overflow_bitfield, /* Complain_on_overflow. */
  101. 0, /* Special_function. */
  102. "REFQUAD", /* Name. */
  103. TRUE, /* Partial_inplace. */
  104. ONES (64), /* Source mask. */
  105. ONES (64), /* Dest mask. */
  106. FALSE), /* PCrel_offset. */
  107. /* A 32 bit GP relative offset. This is just like REFLONG except
  108. that when the value is used the value of the gp register will be
  109. added in. */
  110. HOWTO (ALPHA_R_GPREL32, /* Type. */
  111. 0, /* Rightshift. */
  112. 2, /* Size (0 = byte, 1 = short, 2 = long). */
  113. 32, /* Bitsize. */
  114. FALSE, /* PC_relative. */
  115. 0, /* Bitpos. */
  116. complain_overflow_bitfield, /* Complain_on_overflow. */
  117. 0, /* Special_function. */
  118. "GPREL32", /* Name. */
  119. TRUE, /* Partial_inplace. */
  120. 0xffffffff, /* Source mask. */
  121. 0xffffffff, /* Dest mask. */
  122. FALSE), /* PCrel_offset. */
  123. /* Used for an instruction that refers to memory off the GP
  124. register. The offset is 16 bits of the 32 bit instruction. This
  125. reloc always seems to be against the .lita section. */
  126. HOWTO (ALPHA_R_LITERAL, /* Type. */
  127. 0, /* Rightshift. */
  128. 2, /* Size (0 = byte, 1 = short, 2 = long). */
  129. 16, /* Bitsize. */
  130. FALSE, /* PC_relative. */
  131. 0, /* Bitpos. */
  132. complain_overflow_signed, /* Complain_on_overflow. */
  133. 0, /* Special_function. */
  134. "LITERAL", /* Name. */
  135. TRUE, /* Partial_inplace. */
  136. 0xffff, /* Source mask. */
  137. 0xffff, /* Dest mask. */
  138. FALSE), /* PCrel_offset. */
  139. /* This reloc only appears immediately following a LITERAL reloc.
  140. It identifies a use of the literal. It seems that the linker can
  141. use this to eliminate a portion of the .lita section. The symbol
  142. index is special: 1 means the literal address is in the base
  143. register of a memory format instruction; 2 means the literal
  144. address is in the byte offset register of a byte-manipulation
  145. instruction; 3 means the literal address is in the target
  146. register of a jsr instruction. This does not actually do any
  147. relocation. */
  148. HOWTO (ALPHA_R_LITUSE, /* Type. */
  149. 0, /* Rightshift. */
  150. 2, /* Size (0 = byte, 1 = short, 2 = long). */
  151. 32, /* Bitsize. */
  152. FALSE, /* PC_relative. */
  153. 0, /* Bitpos. */
  154. complain_overflow_dont, /* Complain_on_overflow. */
  155. 0, /* Special_function. */
  156. "LITUSE", /* Name. */
  157. FALSE, /* Partial_inplace. */
  158. 0, /* Source mask. */
  159. 0, /* Dest mask. */
  160. FALSE), /* PCrel_offset. */
  161. /* Load the gp register. This is always used for a ldah instruction
  162. which loads the upper 16 bits of the gp register. The next reloc
  163. will be an IGNORE reloc which identifies the location of the lda
  164. instruction which loads the lower 16 bits. The symbol index of
  165. the GPDISP instruction appears to actually be the number of bytes
  166. between the ldah and lda instructions. This gives two different
  167. ways to determine where the lda instruction is; I don't know why
  168. both are used. The value to use for the relocation is the
  169. difference between the GP value and the current location; the
  170. load will always be done against a register holding the current
  171. address. */
  172. HOWTO (ALPHA_R_GPDISP, /* Type. */
  173. 16, /* Rightshift. */
  174. 2, /* Size (0 = byte, 1 = short, 2 = long). */
  175. 16, /* Bitsize. */
  176. TRUE, /* PC_relative. */
  177. 0, /* Bitpos. */
  178. complain_overflow_dont, /* Complain_on_overflow. */
  179. 0, /* Special_function. */
  180. "GPDISP", /* Name. */
  181. TRUE, /* Partial_inplace. */
  182. 0xffff, /* Source mask. */
  183. 0xffff, /* Dest mask. */
  184. TRUE), /* PCrel_offset. */
  185. /* A 21 bit branch. The native assembler generates these for
  186. branches within the text segment, and also fills in the PC
  187. relative offset in the instruction. It seems to me that this
  188. reloc, unlike the others, is not partial_inplace. */
  189. HOWTO (ALPHA_R_BRADDR, /* Type. */
  190. 2, /* Rightshift. */
  191. 2, /* Size (0 = byte, 1 = short, 2 = long). */
  192. 21, /* Bitsize. */
  193. TRUE, /* PC_relative. */
  194. 0, /* Bitpos. */
  195. complain_overflow_signed, /* Complain_on_overflow. */
  196. 0, /* Special_function. */
  197. "BRADDR", /* Name. */
  198. FALSE, /* Partial_inplace. */
  199. 0, /* Source mask. */
  200. 0x1fffff, /* Dest mask. */
  201. FALSE), /* PCrel_offset. */
  202. /* A hint for a jump to a register. */
  203. HOWTO (ALPHA_R_HINT, /* Type. */
  204. 2, /* Rightshift. */
  205. 2, /* Size (0 = byte, 1 = short, 2 = long). */
  206. 14, /* Bitsize. */
  207. FALSE, /* PC_relative. */
  208. 0, /* Bitpos. */
  209. complain_overflow_dont, /* Complain_on_overflow. */
  210. 0, /* Special_function. */
  211. "HINT", /* Name. */
  212. TRUE, /* Partial_inplace. */
  213. 0x3fff, /* Source mask. */
  214. 0x3fff, /* Dest mask. */
  215. FALSE), /* PCrel_offset. */
  216. /* 16 bit PC relative offset. */
  217. HOWTO (ALPHA_R_SREL16, /* Type. */
  218. 0, /* Rightshift. */
  219. 1, /* Size (0 = byte, 1 = short, 2 = long). */
  220. 16, /* Bitsize. */
  221. TRUE, /* PC_relative. */
  222. 0, /* Bitpos. */
  223. complain_overflow_signed, /* Complain_on_overflow. */
  224. 0, /* Special_function. */
  225. "SREL16", /* Name. */
  226. TRUE, /* Partial_inplace. */
  227. 0xffff, /* Source mask. */
  228. 0xffff, /* Dest mask. */
  229. FALSE), /* PCrel_offset. */
  230. /* 32 bit PC relative offset. */
  231. HOWTO (ALPHA_R_SREL32, /* Type. */
  232. 0, /* Rightshift. */
  233. 2, /* Size (0 = byte, 1 = short, 2 = long). */
  234. 32, /* Bitsize. */
  235. TRUE, /* PC_relative. */
  236. 0, /* Bitpos. */
  237. complain_overflow_signed, /* Complain_on_overflow. */
  238. 0, /* Special_function. */
  239. "SREL32", /* Name. */
  240. TRUE, /* Partial_inplace. */
  241. 0xffffffff, /* Source mask. */
  242. 0xffffffff, /* Dest mask. */
  243. FALSE), /* PCrel_offset. */
  244. /* A 64 bit PC relative offset. */
  245. HOWTO (ALPHA_R_SREL64, /* Type. */
  246. 0, /* Rightshift. */
  247. 4, /* Size (0 = byte, 1 = short, 2 = long). */
  248. 64, /* Bitsize. */
  249. TRUE, /* PC_relative. */
  250. 0, /* Bitpos. */
  251. complain_overflow_signed, /* Complain_on_overflow. */
  252. 0, /* Special_function. */
  253. "SREL64", /* Name. */
  254. TRUE, /* Partial_inplace. */
  255. ONES (64), /* Source mask. */
  256. ONES (64), /* Dest mask. */
  257. FALSE), /* PCrel_offset. */
  258. /* Push a value on the reloc evaluation stack. */
  259. HOWTO (ALPHA_R_OP_PUSH, /* Type. */
  260. 0, /* Rightshift. */
  261. 0, /* Size (0 = byte, 1 = short, 2 = long). */
  262. 0, /* Bitsize. */
  263. FALSE, /* PC_relative. */
  264. 0, /* Bitpos. */
  265. complain_overflow_dont, /* Complain_on_overflow. */
  266. 0, /* Special_function. */
  267. "OP_PUSH", /* Name. */
  268. FALSE, /* Partial_inplace. */
  269. 0, /* Source mask. */
  270. 0, /* Dest mask. */
  271. FALSE), /* PCrel_offset. */
  272. /* Store the value from the stack at the given address. Store it in
  273. a bitfield of size r_size starting at bit position r_offset. */
  274. HOWTO (ALPHA_R_OP_STORE, /* Type. */
  275. 0, /* Rightshift. */
  276. 4, /* Size (0 = byte, 1 = short, 2 = long). */
  277. 64, /* Bitsize. */
  278. FALSE, /* PC_relative. */
  279. 0, /* Bitpos. */
  280. complain_overflow_dont, /* Complain_on_overflow. */
  281. 0, /* Special_function. */
  282. "OP_STORE", /* Name. */
  283. FALSE, /* Partial_inplace. */
  284. 0, /* Source mask. */
  285. ONES (64), /* Dest mask. */
  286. FALSE), /* PCrel_offset. */
  287. /* Subtract the reloc address from the value on the top of the
  288. relocation stack. */
  289. HOWTO (ALPHA_R_OP_PSUB, /* Type. */
  290. 0, /* Rightshift. */
  291. 0, /* Size (0 = byte, 1 = short, 2 = long). */
  292. 0, /* Bitsize. */
  293. FALSE, /* PC_relative. */
  294. 0, /* Bitpos. */
  295. complain_overflow_dont, /* Complain_on_overflow. */
  296. 0, /* Special_function. */
  297. "OP_PSUB", /* Name. */
  298. FALSE, /* Partial_inplace. */
  299. 0, /* Source mask. */
  300. 0, /* Dest mask. */
  301. FALSE), /* PCrel_offset. */
  302. /* Shift the value on the top of the relocation stack right by the
  303. given value. */
  304. HOWTO (ALPHA_R_OP_PRSHIFT, /* Type. */
  305. 0, /* Rightshift. */
  306. 0, /* Size (0 = byte, 1 = short, 2 = long). */
  307. 0, /* Bitsize. */
  308. FALSE, /* PC_relative. */
  309. 0, /* Bitpos. */
  310. complain_overflow_dont, /* Complain_on_overflow. */
  311. 0, /* Special_function. */
  312. "OP_PRSHIFT", /* Name. */
  313. FALSE, /* Partial_inplace. */
  314. 0, /* Source mask. */
  315. 0, /* Dest mask. */
  316. FALSE), /* PCrel_offset. */
  317. /* Adjust the GP value for a new range in the object file. */
  318. HOWTO (ALPHA_R_GPVALUE, /* Type. */
  319. 0, /* Rightshift. */
  320. 0, /* Size (0 = byte, 1 = short, 2 = long). */
  321. 0, /* Bitsize. */
  322. FALSE, /* PC_relative. */
  323. 0, /* Bitpos. */
  324. complain_overflow_dont, /* Complain_on_overflow. */
  325. 0, /* Special_function. */
  326. "GPVALUE", /* Name. */
  327. FALSE, /* Partial_inplace. */
  328. 0, /* Source mask. */
  329. 0, /* Dest mask. */
  330. FALSE) /* PCrel_offset. */
  331. };
  332. static reloc_howto_type nlm32_alpha_nw_howto =
  333. HOWTO (ALPHA_R_NW_RELOC, /* Type. */
  334. 0, /* Rightshift. */
  335. 0, /* Size (0 = byte, 1 = short, 2 = long). */
  336. 0, /* Bitsize. */
  337. FALSE, /* PC_relative. */
  338. 0, /* Bitpos. */
  339. complain_overflow_dont, /* Complain_on_overflow. */
  340. 0, /* Special_function. */
  341. "NW_RELOC", /* Name. */
  342. FALSE, /* Partial_inplace. */
  343. 0, /* Source mask. */
  344. 0, /* Dest mask. */
  345. FALSE); /* PCrel_offset. */
  346. /* Read an Alpha NLM reloc. This routine keeps some static data which
  347. it uses when handling local relocs. This only works correctly
  348. because all the local relocs are read at once. */
  349. static bfd_boolean
  350. nlm_alpha_read_reloc (bfd *abfd,
  351. nlmNAME (symbol_type) *sym,
  352. asection **secp,
  353. arelent *rel)
  354. {
  355. static bfd_vma gp_value;
  356. static bfd_vma lita_address;
  357. struct nlm32_alpha_external_reloc ext;
  358. bfd_vma r_vaddr;
  359. long r_symndx;
  360. int r_type, r_extern, r_offset, r_size;
  361. asection *code_sec, *data_sec;
  362. /* Read the reloc from the file. */
  363. if (bfd_bread (&ext, (bfd_size_type) sizeof ext, abfd) != sizeof ext)
  364. return FALSE;
  365. /* Swap in the reloc information. */
  366. r_vaddr = H_GET_64 (abfd, ext.r_vaddr);
  367. r_symndx = H_GET_32 (abfd, ext.r_symndx);
  368. BFD_ASSERT (bfd_little_endian (abfd));
  369. r_type = ((ext.r_bits[0] & RELOC_BITS0_TYPE_LITTLE)
  370. >> RELOC_BITS0_TYPE_SH_LITTLE);
  371. r_extern = (ext.r_bits[1] & RELOC_BITS1_EXTERN_LITTLE) != 0;
  372. r_offset = ((ext.r_bits[1] & RELOC_BITS1_OFFSET_LITTLE)
  373. >> RELOC_BITS1_OFFSET_SH_LITTLE);
  374. /* Ignore the reserved bits. */
  375. r_size = ((ext.r_bits[3] & RELOC_BITS3_SIZE_LITTLE)
  376. >> RELOC_BITS3_SIZE_SH_LITTLE);
  377. /* Fill in the BFD arelent structure. */
  378. code_sec = bfd_get_section_by_name (abfd, NLM_CODE_NAME);
  379. data_sec = bfd_get_section_by_name (abfd, NLM_INITIALIZED_DATA_NAME);
  380. if (r_extern)
  381. {
  382. /* External relocations are only used for imports. */
  383. BFD_ASSERT (sym != NULL);
  384. /* We don't need to set sym_ptr_ptr for this case. It is set in
  385. nlm_canonicalize_reloc. */
  386. rel->sym_ptr_ptr = NULL;
  387. rel->addend = 0;
  388. }
  389. else
  390. {
  391. /* Internal relocations are only used for local relocation
  392. fixups. If they are not NW_RELOC or GPDISP or IGNORE, they
  393. must be against .text or .data. */
  394. BFD_ASSERT (r_type == ALPHA_R_NW_RELOC || sym == NULL);
  395. if (r_type == ALPHA_R_NW_RELOC
  396. || r_type == ALPHA_R_GPDISP
  397. || r_type == ALPHA_R_IGNORE)
  398. {
  399. rel->sym_ptr_ptr = bfd_abs_section_ptr->symbol_ptr_ptr;
  400. rel->addend = 0;
  401. }
  402. else if (r_symndx == ALPHA_RELOC_SECTION_TEXT)
  403. {
  404. rel->sym_ptr_ptr = code_sec->symbol_ptr_ptr;
  405. BFD_ASSERT (bfd_get_section_vma (abfd, code_sec) == 0);
  406. rel->addend = 0;
  407. }
  408. else if (r_symndx == ALPHA_RELOC_SECTION_DATA)
  409. {
  410. rel->sym_ptr_ptr = data_sec->symbol_ptr_ptr;
  411. rel->addend = - bfd_get_section_vma (abfd, data_sec);
  412. }
  413. else
  414. {
  415. BFD_ASSERT (0);
  416. rel->sym_ptr_ptr = bfd_abs_section_ptr->symbol_ptr_ptr;
  417. rel->addend = 0;
  418. }
  419. }
  420. /* We use the address to determine whether the reloc is in the .text
  421. or .data section. R_NW_RELOC relocs don't really have a section,
  422. so we put them in .text. */
  423. if (r_type == ALPHA_R_NW_RELOC
  424. || r_vaddr < code_sec->size)
  425. {
  426. *secp = code_sec;
  427. rel->address = r_vaddr;
  428. }
  429. else
  430. {
  431. *secp = data_sec;
  432. rel->address = r_vaddr - code_sec->size;
  433. }
  434. /* We must adjust the addend based on the type. */
  435. BFD_ASSERT ((r_type >= 0 && r_type <= ALPHA_R_GPVALUE)
  436. || r_type == ALPHA_R_NW_RELOC);
  437. switch (r_type)
  438. {
  439. case ALPHA_R_BRADDR:
  440. case ALPHA_R_SREL16:
  441. case ALPHA_R_SREL32:
  442. case ALPHA_R_SREL64:
  443. /* The PC relative relocs do not seem to use the section VMA as
  444. a negative addend. */
  445. rel->addend = 0;
  446. break;
  447. case ALPHA_R_GPREL32:
  448. /* Copy the gp value for this object file into the addend, to
  449. ensure that we are not confused by the linker. */
  450. if (! r_extern)
  451. rel->addend += gp_value;
  452. break;
  453. case ALPHA_R_LITERAL:
  454. BFD_ASSERT (! r_extern);
  455. rel->addend += lita_address;
  456. break;
  457. case ALPHA_R_LITUSE:
  458. case ALPHA_R_GPDISP:
  459. /* The LITUSE and GPDISP relocs do not use a symbol, or an
  460. addend, but they do use a special code. Put this code in the
  461. addend field. */
  462. rel->addend = r_symndx;
  463. rel->sym_ptr_ptr = bfd_abs_section_ptr->symbol_ptr_ptr;
  464. break;
  465. case ALPHA_R_OP_STORE:
  466. /* The STORE reloc needs the size and offset fields. We store
  467. them in the addend. */
  468. BFD_ASSERT (r_offset < 256 && r_size < 256);
  469. rel->addend = (r_offset << 8) + r_size;
  470. break;
  471. case ALPHA_R_OP_PUSH:
  472. case ALPHA_R_OP_PSUB:
  473. case ALPHA_R_OP_PRSHIFT:
  474. /* The PUSH, PSUB and PRSHIFT relocs do not actually use an
  475. address. I believe that the address supplied is really an
  476. addend. */
  477. rel->addend = r_vaddr;
  478. break;
  479. case ALPHA_R_GPVALUE:
  480. /* Record the new gp value. */
  481. gp_value += r_symndx;
  482. rel->addend = gp_value;
  483. break;
  484. case ALPHA_R_IGNORE:
  485. /* If the type is ALPHA_R_IGNORE, make sure this is a reference
  486. to the absolute section so that the reloc is ignored. For
  487. some reason the address of this reloc type is not adjusted by
  488. the section vma. We record the gp value for this object file
  489. here, for convenience when doing the GPDISP relocation. */
  490. rel->sym_ptr_ptr = bfd_abs_section_ptr->symbol_ptr_ptr;
  491. rel->address = r_vaddr;
  492. rel->addend = gp_value;
  493. break;
  494. case ALPHA_R_NW_RELOC:
  495. /* If this is SETGP, we set the addend to 0. Otherwise we set
  496. the addend to the size of the .lita section (this is
  497. r_symndx) plus 1. We have already set the address of the
  498. reloc to r_vaddr. */
  499. if (r_size == ALPHA_R_NW_RELOC_SETGP)
  500. {
  501. gp_value = r_vaddr;
  502. rel->addend = 0;
  503. }
  504. else if (r_size == ALPHA_R_NW_RELOC_LITA)
  505. {
  506. lita_address = r_vaddr;
  507. rel->addend = r_symndx + 1;
  508. }
  509. else
  510. BFD_ASSERT (0);
  511. rel->sym_ptr_ptr = bfd_abs_section_ptr->symbol_ptr_ptr;
  512. break;
  513. default:
  514. break;
  515. }
  516. if (r_type == ALPHA_R_NW_RELOC)
  517. rel->howto = &nlm32_alpha_nw_howto;
  518. else
  519. rel->howto = &nlm32_alpha_howto_table[r_type];
  520. return TRUE;
  521. }
  522. /* Mangle Alpha NLM relocs for output. */
  523. static bfd_boolean
  524. nlm_alpha_mangle_relocs (bfd *abfd ATTRIBUTE_UNUSED,
  525. asection *sec ATTRIBUTE_UNUSED,
  526. const void * data ATTRIBUTE_UNUSED,
  527. bfd_vma offset ATTRIBUTE_UNUSED,
  528. bfd_size_type count ATTRIBUTE_UNUSED)
  529. {
  530. return TRUE;
  531. }
  532. /* Read an ALPHA NLM import record. */
  533. static bfd_boolean
  534. nlm_alpha_read_import (bfd *abfd, nlmNAME (symbol_type) * sym)
  535. {
  536. struct nlm_relent *nlm_relocs; /* Relocation records for symbol. */
  537. bfd_size_type rcount; /* Number of relocs. */
  538. bfd_byte temp[NLM_TARGET_LONG_SIZE]; /* Temporary 32-bit value. */
  539. unsigned char symlength; /* Length of symbol name. */
  540. char *name;
  541. bfd_size_type amt;
  542. if (bfd_bread (& symlength, (bfd_size_type) sizeof (symlength), abfd)
  543. != sizeof (symlength))
  544. return FALSE;
  545. sym -> symbol.the_bfd = abfd;
  546. name = bfd_alloc (abfd, (bfd_size_type) symlength + 1);
  547. if (name == NULL)
  548. return FALSE;
  549. if (bfd_bread (name, (bfd_size_type) symlength, abfd) != symlength)
  550. return FALSE;
  551. name[symlength] = '\0';
  552. sym -> symbol.name = name;
  553. sym -> symbol.flags = 0;
  554. sym -> symbol.value = 0;
  555. sym -> symbol.section = bfd_und_section_ptr;
  556. if (bfd_bread (temp, (bfd_size_type) sizeof (temp), abfd)
  557. != sizeof (temp))
  558. return FALSE;
  559. rcount = H_GET_32 (abfd, temp);
  560. amt = rcount * sizeof (struct nlm_relent);
  561. nlm_relocs = bfd_alloc (abfd, amt);
  562. if (!nlm_relocs)
  563. return FALSE;
  564. sym -> relocs = nlm_relocs;
  565. sym -> rcnt = 0;
  566. while (sym -> rcnt < rcount)
  567. {
  568. asection *section;
  569. if (! nlm_alpha_read_reloc (abfd, sym, &section, &nlm_relocs -> reloc))
  570. return FALSE;
  571. nlm_relocs -> section = section;
  572. nlm_relocs++;
  573. sym -> rcnt++;
  574. }
  575. return TRUE;
  576. }
  577. /* Write an Alpha NLM reloc. */
  578. static bfd_boolean
  579. nlm_alpha_write_import (bfd * abfd, asection * sec, arelent * rel)
  580. {
  581. asymbol *sym;
  582. bfd_vma r_vaddr;
  583. long r_symndx;
  584. int r_type, r_extern, r_offset, r_size;
  585. struct nlm32_alpha_external_reloc ext;
  586. sym = *rel->sym_ptr_ptr;
  587. /* Get values for the relocation fields. */
  588. r_type = rel->howto->type;
  589. if (r_type != ALPHA_R_NW_RELOC)
  590. {
  591. r_vaddr = bfd_get_section_vma (abfd, sec) + rel->address;
  592. if ((sec->flags & SEC_CODE) == 0)
  593. r_vaddr += bfd_get_section_by_name (abfd, NLM_CODE_NAME) -> size;
  594. if (bfd_is_und_section (bfd_get_section (sym)))
  595. {
  596. r_extern = 1;
  597. r_symndx = 0;
  598. }
  599. else
  600. {
  601. r_extern = 0;
  602. if (bfd_get_section_flags (abfd, bfd_get_section (sym)) & SEC_CODE)
  603. r_symndx = ALPHA_RELOC_SECTION_TEXT;
  604. else
  605. r_symndx = ALPHA_RELOC_SECTION_DATA;
  606. }
  607. r_offset = 0;
  608. r_size = 0;
  609. switch (r_type)
  610. {
  611. case ALPHA_R_LITUSE:
  612. case ALPHA_R_GPDISP:
  613. r_symndx = rel->addend;
  614. break;
  615. case ALPHA_R_OP_STORE:
  616. r_size = rel->addend & 0xff;
  617. r_offset = (rel->addend >> 8) & 0xff;
  618. break;
  619. case ALPHA_R_OP_PUSH:
  620. case ALPHA_R_OP_PSUB:
  621. case ALPHA_R_OP_PRSHIFT:
  622. r_vaddr = rel->addend;
  623. break;
  624. case ALPHA_R_IGNORE:
  625. r_vaddr = rel->address;
  626. break;
  627. default:
  628. break;
  629. }
  630. }
  631. else
  632. {
  633. /* r_type == ALPHA_R_NW_RELOC. */
  634. r_vaddr = rel->address;
  635. if (rel->addend == 0)
  636. {
  637. r_symndx = 0;
  638. r_size = ALPHA_R_NW_RELOC_SETGP;
  639. }
  640. else
  641. {
  642. r_symndx = rel->addend - 1;
  643. r_size = ALPHA_R_NW_RELOC_LITA;
  644. }
  645. r_extern = 0;
  646. r_offset = 0;
  647. }
  648. /* Swap out the relocation fields. */
  649. H_PUT_64 (abfd, r_vaddr, ext.r_vaddr);
  650. H_PUT_32 (abfd, r_symndx, ext.r_symndx);
  651. BFD_ASSERT (bfd_little_endian (abfd));
  652. ext.r_bits[0] = ((r_type << RELOC_BITS0_TYPE_SH_LITTLE)
  653. & RELOC_BITS0_TYPE_LITTLE);
  654. ext.r_bits[1] = ((r_extern ? RELOC_BITS1_EXTERN_LITTLE : 0)
  655. | ((r_offset << RELOC_BITS1_OFFSET_SH_LITTLE)
  656. & RELOC_BITS1_OFFSET_LITTLE));
  657. ext.r_bits[2] = 0;
  658. ext.r_bits[3] = ((r_size << RELOC_BITS3_SIZE_SH_LITTLE)
  659. & RELOC_BITS3_SIZE_LITTLE);
  660. /* Write out the relocation. */
  661. if (bfd_bwrite (&ext, (bfd_size_type) sizeof ext, abfd) != sizeof ext)
  662. return FALSE;
  663. return TRUE;
  664. }
  665. /* Alpha NetWare does not use the high bit to determine whether a
  666. public symbol is in the code segment or the data segment. Instead,
  667. it just uses the address. The set_public_section and
  668. get_public_offset routines override the default code which uses the
  669. high bit. */
  670. /* Set the section for a public symbol. */
  671. static bfd_boolean
  672. nlm_alpha_set_public_section (bfd * abfd, nlmNAME (symbol_type) * sym)
  673. {
  674. asection *code_sec, *data_sec;
  675. code_sec = bfd_get_section_by_name (abfd, NLM_CODE_NAME);
  676. data_sec = bfd_get_section_by_name (abfd, NLM_INITIALIZED_DATA_NAME);
  677. if (sym->symbol.value < code_sec->size)
  678. {
  679. sym->symbol.section = code_sec;
  680. sym->symbol.flags |= BSF_FUNCTION;
  681. }
  682. else
  683. {
  684. sym->symbol.section = data_sec;
  685. sym->symbol.value -= code_sec->size;
  686. /* The data segment had better be aligned. */
  687. BFD_ASSERT ((code_sec->size & 0xf) == 0);
  688. }
  689. return TRUE;
  690. }
  691. /* Get the offset to write out for a public symbol. */
  692. static bfd_vma
  693. nlm_alpha_get_public_offset (bfd * abfd ATTRIBUTE_UNUSED, asymbol * sym)
  694. {
  695. return bfd_asymbol_value (sym);
  696. }
  697. /* Write an Alpha NLM external symbol. */
  698. static bfd_boolean
  699. nlm_alpha_write_external (bfd *abfd,
  700. bfd_size_type count,
  701. asymbol *sym,
  702. struct reloc_and_sec *relocs)
  703. {
  704. bfd_size_type i;
  705. bfd_byte len;
  706. unsigned char temp[NLM_TARGET_LONG_SIZE];
  707. arelent r;
  708. len = strlen (sym->name);
  709. if ((bfd_bwrite (&len, (bfd_size_type) sizeof (bfd_byte), abfd)
  710. != sizeof (bfd_byte))
  711. || bfd_bwrite (sym->name, (bfd_size_type) len, abfd) != len)
  712. return FALSE;
  713. bfd_put_32 (abfd, count + 2, temp);
  714. if (bfd_bwrite (temp, (bfd_size_type) sizeof (temp), abfd) != sizeof (temp))
  715. return FALSE;
  716. /* The first two relocs for each external symbol are the .lita
  717. address and the GP value. */
  718. r.sym_ptr_ptr = bfd_abs_section_ptr->symbol_ptr_ptr;
  719. r.howto = &nlm32_alpha_nw_howto;
  720. r.address = nlm_alpha_backend_data (abfd)->lita_address;
  721. r.addend = nlm_alpha_backend_data (abfd)->lita_size + 1;
  722. if (! nlm_alpha_write_import (abfd, NULL, &r))
  723. return FALSE;
  724. r.address = nlm_alpha_backend_data (abfd)->gp;
  725. r.addend = 0;
  726. if (! nlm_alpha_write_import (abfd, NULL, &r))
  727. return FALSE;
  728. for (i = 0; i < count; i++)
  729. if (! nlm_alpha_write_import (abfd, relocs[i].sec, relocs[i].rel))
  730. return FALSE;
  731. return TRUE;
  732. }
  733. #include "nlmswap.h"
  734. static const struct nlm_backend_data nlm32_alpha_backend =
  735. {
  736. "NetWare Alpha Module \032",
  737. sizeof (Nlm32_alpha_External_Fixed_Header),
  738. sizeof (struct nlm32_alpha_external_prefix_header),
  739. bfd_arch_alpha,
  740. 0,
  741. TRUE, /* No uninitialized data permitted by Alpha NetWare. */
  742. nlm_alpha_backend_object_p,
  743. nlm_alpha_write_prefix,
  744. nlm_alpha_read_reloc,
  745. nlm_alpha_mangle_relocs,
  746. nlm_alpha_read_import,
  747. nlm_alpha_write_import,
  748. nlm_alpha_set_public_section,
  749. nlm_alpha_get_public_offset,
  750. nlm_swap_fixed_header_in,
  751. nlm_swap_fixed_header_out,
  752. nlm_alpha_write_external,
  753. 0, /* Write_export. */
  754. };
  755. #define TARGET_LITTLE_NAME "nlm32-alpha"
  756. #define TARGET_LITTLE_SYM alpha_nlm32_vec
  757. #define TARGET_BACKEND_DATA & nlm32_alpha_backend
  758. #include "nlm-target.h"