nlm32-i386.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. /* Support for 32-bit i386 NLM (NetWare Loadable Module)
  2. Copyright (C) 1993-2015 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. #include "sysdep.h"
  17. #include "bfd.h"
  18. #include "libbfd.h"
  19. #define ARCH_SIZE 32
  20. #include "nlm/i386-ext.h"
  21. #define Nlm_External_Fixed_Header Nlm32_i386_External_Fixed_Header
  22. #include "libnlm.h"
  23. /* Adjust the reloc location by an absolute value. */
  24. static reloc_howto_type nlm_i386_abs_howto =
  25. HOWTO (0, /* Type. */
  26. 0, /* Rightshift. */
  27. 2, /* Size (0 = byte, 1 = short, 2 = long). */
  28. 32, /* Bitsize. */
  29. FALSE, /* PC relative. */
  30. 0, /* Bitpos. */
  31. complain_overflow_bitfield, /* Complain_on_overflow. */
  32. 0, /* Special_function. */
  33. "32", /* Name. */
  34. TRUE, /* Partial_inplace. */
  35. 0xffffffff, /* Source mask. */
  36. 0xffffffff, /* Dest mask. */
  37. FALSE); /* PR rel_offset. */
  38. /* Adjust the reloc location by a PC relative displacement. */
  39. static reloc_howto_type nlm_i386_pcrel_howto =
  40. HOWTO (1, /* Type. */
  41. 0, /* Rightshift. */
  42. 2, /* Size (0 = byte, 1 = short, 2 = long). */
  43. 32, /* Bitsize. */
  44. TRUE, /* PC relative. */
  45. 0, /* Bitpos. */
  46. complain_overflow_signed, /* Complain_on_overflow. */
  47. 0, /* Special_function. */
  48. "DISP32", /* Name. */
  49. TRUE, /* Partial_inplace. */
  50. 0xffffffff, /* Source mask. */
  51. 0xffffffff, /* Dest mask. */
  52. TRUE); /* PR rel_offset. */
  53. /* Read a NetWare i386 reloc. */
  54. static bfd_boolean
  55. nlm_i386_read_reloc (bfd *abfd,
  56. nlmNAME (symbol_type) *sym,
  57. asection **secp,
  58. arelent *rel)
  59. {
  60. bfd_byte temp[4];
  61. bfd_vma val;
  62. const char *name;
  63. if (bfd_bread (temp, (bfd_size_type) sizeof (temp), abfd) != sizeof (temp))
  64. return FALSE;
  65. val = bfd_get_32 (abfd, temp);
  66. /* The value is an offset into either the code or data segment.
  67. This is the location which needs to be adjusted.
  68. If this is a relocation fixup rather than an imported symbol (the
  69. sym argument is NULL) then the high bit is 0 if the location
  70. needs to be adjusted by the address of the data segment, or 1 if
  71. the location needs to be adjusted by the address of the code
  72. segment. If this is an imported symbol, then the high bit is 0
  73. if the location is 0 if the location should be adjusted by the
  74. offset to the symbol, or 1 if the location should adjusted by the
  75. absolute value of the symbol.
  76. The second most significant bit is 0 if the value is an offset
  77. into the data segment, or 1 if the value is an offset into the
  78. code segment.
  79. All this translates fairly easily into a BFD reloc. */
  80. if (sym == NULL)
  81. {
  82. if ((val & NLM_HIBIT) == 0)
  83. name = NLM_INITIALIZED_DATA_NAME;
  84. else
  85. {
  86. name = NLM_CODE_NAME;
  87. val &=~ NLM_HIBIT;
  88. }
  89. rel->sym_ptr_ptr = bfd_get_section_by_name (abfd, name)->symbol_ptr_ptr;
  90. rel->howto = &nlm_i386_abs_howto;
  91. }
  92. else
  93. {
  94. /* In this case we do not need to set the sym_ptr_ptr field. */
  95. rel->sym_ptr_ptr = NULL;
  96. if ((val & NLM_HIBIT) == 0)
  97. rel->howto = &nlm_i386_pcrel_howto;
  98. else
  99. {
  100. rel->howto = &nlm_i386_abs_howto;
  101. val &=~ NLM_HIBIT;
  102. }
  103. }
  104. if ((val & (NLM_HIBIT >> 1)) == 0)
  105. *secp = bfd_get_section_by_name (abfd, NLM_INITIALIZED_DATA_NAME);
  106. else
  107. {
  108. *secp = bfd_get_section_by_name (abfd, NLM_CODE_NAME);
  109. val &=~ (NLM_HIBIT >> 1);
  110. }
  111. rel->address = val;
  112. rel->addend = 0;
  113. return TRUE;
  114. }
  115. /* Write a NetWare i386 reloc. */
  116. static bfd_boolean
  117. nlm_i386_write_import (bfd * abfd, asection * sec, arelent * rel)
  118. {
  119. asymbol *sym;
  120. bfd_vma val;
  121. bfd_byte temp[4];
  122. /* NetWare only supports two kinds of relocs. We should check
  123. special_function here, as well, but at the moment coff-i386
  124. relocs uses a special_function which does not affect what we do
  125. here. */
  126. if (rel->addend != 0
  127. || rel->howto == NULL
  128. || rel->howto->rightshift != 0
  129. || rel->howto->size != 2
  130. || rel->howto->bitsize != 32
  131. || rel->howto->bitpos != 0
  132. || rel->howto->src_mask != 0xffffffff
  133. || rel->howto->dst_mask != 0xffffffff)
  134. {
  135. bfd_set_error (bfd_error_invalid_operation);
  136. return FALSE;
  137. }
  138. sym = *rel->sym_ptr_ptr;
  139. /* The value we write out is the offset into the appropriate
  140. segment. This offset is the section vma, adjusted by the vma of
  141. the lowest section in that segment, plus the address of the
  142. relocation. */
  143. val = bfd_get_section_vma (abfd, sec) + rel->address;
  144. /* The second most significant bit is 0 if the value is an offset
  145. into the data segment, or 1 if the value is an offset into the
  146. code segment. */
  147. if (bfd_get_section_flags (abfd, sec) & SEC_CODE)
  148. {
  149. val -= nlm_get_text_low (abfd);
  150. val |= NLM_HIBIT >> 1;
  151. }
  152. else
  153. val -= nlm_get_data_low (abfd);
  154. if (! bfd_is_und_section (bfd_get_section (sym)))
  155. {
  156. /* NetWare only supports absolute internal relocs. */
  157. if (rel->howto->pc_relative)
  158. {
  159. bfd_set_error (bfd_error_invalid_operation);
  160. return FALSE;
  161. }
  162. /* The high bit is 1 if the reloc is against the code section, 0
  163. if against the data section. */
  164. if (bfd_get_section_flags (abfd, bfd_get_section (sym)) & SEC_CODE)
  165. val |= NLM_HIBIT;
  166. }
  167. else
  168. {
  169. /* The high bit is 1 if this is an absolute reloc, 0 if it is PC
  170. relative. */
  171. if (! rel->howto->pc_relative)
  172. val |= NLM_HIBIT;
  173. else
  174. {
  175. /* PC relative relocs on NetWare must be pcrel_offset. */
  176. if (! rel->howto->pcrel_offset)
  177. {
  178. bfd_set_error (bfd_error_invalid_operation);
  179. return FALSE;
  180. }
  181. }
  182. }
  183. bfd_put_32 (abfd, val, temp);
  184. if (bfd_bwrite (temp, (bfd_size_type) sizeof (temp), abfd) != sizeof (temp))
  185. return FALSE;
  186. return TRUE;
  187. }
  188. /* I want to be able to use objcopy to turn an i386 a.out or COFF file
  189. into a NetWare i386 module. That means that the relocs from the
  190. source file have to be mapped into relocs that apply to the target
  191. file. This function is called by nlm_set_section_contents to give
  192. it a chance to rework the relocs.
  193. This is actually a fairly general concept. However, this is not a
  194. general implementation. */
  195. static bfd_boolean
  196. nlm_i386_mangle_relocs (bfd *abfd,
  197. asection *sec,
  198. const void * data,
  199. bfd_vma offset,
  200. bfd_size_type count)
  201. {
  202. arelent **rel_ptr_ptr, **rel_end;
  203. rel_ptr_ptr = sec->orelocation;
  204. rel_end = rel_ptr_ptr + sec->reloc_count;
  205. for (; rel_ptr_ptr < rel_end; rel_ptr_ptr++)
  206. {
  207. arelent *rel;
  208. asymbol *sym;
  209. bfd_vma addend;
  210. rel = *rel_ptr_ptr;
  211. sym = *rel->sym_ptr_ptr;
  212. /* Note that no serious harm will ensue if we fail to change a
  213. reloc. We will wind up failing in nlm_i386_write_import. */
  214. /* Make sure this reloc is within the data we have. We only 4
  215. byte relocs here, so we insist on having 4 bytes. */
  216. if (rel->address < offset
  217. || rel->address + 4 > offset + count)
  218. continue;
  219. /* NetWare doesn't support reloc addends, so we get rid of them
  220. here by simply adding them into the object data. We handle
  221. the symbol value, if any, the same way. */
  222. addend = rel->addend + sym->value;
  223. /* The value of a symbol is the offset into the section. If the
  224. symbol is in the .bss segment, we need to include the size of
  225. the data segment in the offset as well. Fortunately, we know
  226. that at this point the size of the data section is in the NLM
  227. header. */
  228. if (((bfd_get_section_flags (abfd, bfd_get_section (sym))
  229. & SEC_LOAD) == 0)
  230. && ((bfd_get_section_flags (abfd, bfd_get_section (sym))
  231. & SEC_ALLOC) != 0))
  232. addend += nlm_fixed_header (abfd)->dataImageSize;
  233. if (addend != 0
  234. && rel->howto != NULL
  235. && rel->howto->rightshift == 0
  236. && rel->howto->size == 2
  237. && rel->howto->bitsize == 32
  238. && rel->howto->bitpos == 0
  239. && rel->howto->src_mask == 0xffffffff
  240. && rel->howto->dst_mask == 0xffffffff)
  241. {
  242. bfd_vma val;
  243. val = bfd_get_32 (abfd, (bfd_byte *) data + rel->address - offset);
  244. val += addend;
  245. bfd_put_32 (abfd, val, (bfd_byte *) data + rel->address - offset);
  246. rel->addend = 0;
  247. }
  248. /* NetWare uses a reloc with pcrel_offset set. We adjust
  249. pc_relative relocs accordingly. We are going to change the
  250. howto field, so we can only do this if the current one is
  251. compatible. We should check special_function here, but at
  252. the moment coff-i386 uses a special_function which does not
  253. affect what we are doing here. */
  254. if (rel->howto != NULL
  255. && rel->howto->pc_relative
  256. && ! rel->howto->pcrel_offset
  257. && rel->howto->rightshift == 0
  258. && rel->howto->size == 2
  259. && rel->howto->bitsize == 32
  260. && rel->howto->bitpos == 0
  261. && rel->howto->src_mask == 0xffffffff
  262. && rel->howto->dst_mask == 0xffffffff)
  263. {
  264. bfd_vma val;
  265. /* When pcrel_offset is not set, it means that the negative
  266. of the address of the memory location is stored in the
  267. memory location. We must add it back in. */
  268. val = bfd_get_32 (abfd, (bfd_byte *) data + rel->address - offset);
  269. val += rel->address;
  270. bfd_put_32 (abfd, val, (bfd_byte *) data + rel->address - offset);
  271. rel->howto = &nlm_i386_pcrel_howto;
  272. }
  273. }
  274. return TRUE;
  275. }
  276. /* Read a NetWare i386 import record. */
  277. static bfd_boolean
  278. nlm_i386_read_import (bfd * abfd, nlmNAME (symbol_type) * sym)
  279. {
  280. struct nlm_relent *nlm_relocs; /* Relocation records for symbol. */
  281. bfd_size_type rcount; /* Number of relocs. */
  282. bfd_byte temp[NLM_TARGET_LONG_SIZE]; /* Temporary 32-bit value. */
  283. unsigned char symlength; /* Length of symbol name. */
  284. char *name;
  285. if (bfd_bread (& symlength, (bfd_size_type) sizeof (symlength), abfd)
  286. != sizeof (symlength))
  287. return FALSE;
  288. sym -> symbol.the_bfd = abfd;
  289. name = bfd_alloc (abfd, (bfd_size_type) symlength + 1);
  290. if (name == NULL)
  291. return FALSE;
  292. if (bfd_bread (name, (bfd_size_type) symlength, abfd) != symlength)
  293. return FALSE;
  294. name[symlength] = '\0';
  295. sym -> symbol.name = name;
  296. sym -> symbol.flags = 0;
  297. sym -> symbol.value = 0;
  298. sym -> symbol.section = bfd_und_section_ptr;
  299. if (bfd_bread (temp, (bfd_size_type) sizeof (temp), abfd) != sizeof (temp))
  300. return FALSE;
  301. rcount = H_GET_32 (abfd, temp);
  302. nlm_relocs = bfd_alloc (abfd, rcount * sizeof (struct nlm_relent));
  303. if (!nlm_relocs)
  304. return FALSE;
  305. sym -> relocs = nlm_relocs;
  306. sym -> rcnt = 0;
  307. while (sym -> rcnt < rcount)
  308. {
  309. asection *section;
  310. if (! nlm_i386_read_reloc (abfd, sym, &section, &nlm_relocs -> reloc))
  311. return FALSE;
  312. nlm_relocs -> section = section;
  313. nlm_relocs++;
  314. sym -> rcnt++;
  315. }
  316. return TRUE;
  317. }
  318. /* Write out an external reference. */
  319. static bfd_boolean
  320. nlm_i386_write_external (bfd *abfd,
  321. bfd_size_type count,
  322. asymbol *sym,
  323. struct reloc_and_sec *relocs)
  324. {
  325. unsigned int i;
  326. bfd_byte len;
  327. unsigned char temp[NLM_TARGET_LONG_SIZE];
  328. len = strlen (sym->name);
  329. if ((bfd_bwrite (&len, (bfd_size_type) sizeof (bfd_byte), abfd)
  330. != sizeof (bfd_byte))
  331. || bfd_bwrite (sym->name, (bfd_size_type) len, abfd) != len)
  332. return FALSE;
  333. bfd_put_32 (abfd, count, temp);
  334. if (bfd_bwrite (temp, (bfd_size_type) sizeof (temp), abfd) != sizeof (temp))
  335. return FALSE;
  336. for (i = 0; i < count; i++)
  337. if (! nlm_i386_write_import (abfd, relocs[i].sec, relocs[i].rel))
  338. return FALSE;
  339. return TRUE;
  340. }
  341. #include "nlmswap.h"
  342. static const struct nlm_backend_data nlm32_i386_backend =
  343. {
  344. "NetWare Loadable Module\032",
  345. sizeof (Nlm32_i386_External_Fixed_Header),
  346. 0, /* Optional_prefix_size. */
  347. bfd_arch_i386,
  348. 0,
  349. FALSE,
  350. 0, /* Backend_object_p. */
  351. 0, /* Write_prefix_func. */
  352. nlm_i386_read_reloc,
  353. nlm_i386_mangle_relocs,
  354. nlm_i386_read_import,
  355. nlm_i386_write_import,
  356. 0, /* Set_public_section. */
  357. 0, /* Set_public_offset. */
  358. nlm_swap_fixed_header_in,
  359. nlm_swap_fixed_header_out,
  360. nlm_i386_write_external,
  361. 0, /* Write_export. */
  362. };
  363. #define TARGET_LITTLE_NAME "nlm32-i386"
  364. #define TARGET_LITTLE_SYM i386_nlm32_vec
  365. #define TARGET_BACKEND_DATA & nlm32_i386_backend
  366. #include "nlm-target.h"