frags.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. /* frags.c - manage frags -
  2. Copyright (C) 1987-2015 Free Software Foundation, Inc.
  3. This file is part of GAS, the GNU Assembler.
  4. GAS 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, or (at your option)
  7. any later version.
  8. GAS 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 GAS; see the file COPYING. If not, write to the Free
  14. Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
  15. 02110-1301, USA. */
  16. #include "as.h"
  17. #include "subsegs.h"
  18. #include "obstack.h"
  19. extern fragS zero_address_frag;
  20. extern fragS predefined_address_frag;
  21. static int totalfrags;
  22. int
  23. get_frag_count (void)
  24. {
  25. return totalfrags;
  26. }
  27. void
  28. clear_frag_count (void)
  29. {
  30. totalfrags = 0;
  31. }
  32. /* Initialization for frag routines. */
  33. void
  34. frag_init (void)
  35. {
  36. zero_address_frag.fr_type = rs_fill;
  37. predefined_address_frag.fr_type = rs_fill;
  38. }
  39. /* Check that we're not trying to assemble into a section that can't
  40. allocate frags (currently, this is only possible in the absolute
  41. section), or into an mri common. */
  42. static void
  43. frag_alloc_check (const struct obstack *ob)
  44. {
  45. if (ob->chunk_size == 0)
  46. {
  47. as_bad (_("attempt to allocate data in absolute section"));
  48. subseg_set (text_section, 0);
  49. }
  50. if (mri_common_symbol != NULL)
  51. {
  52. as_bad (_("attempt to allocate data in common section"));
  53. mri_common_symbol = NULL;
  54. }
  55. }
  56. /* Allocate a frag on the specified obstack.
  57. Call this routine from everywhere else, so that all the weird alignment
  58. hackery can be done in just one place. */
  59. fragS *
  60. frag_alloc (struct obstack *ob)
  61. {
  62. fragS *ptr;
  63. int oalign;
  64. (void) obstack_alloc (ob, 0);
  65. oalign = obstack_alignment_mask (ob);
  66. obstack_alignment_mask (ob) = 0;
  67. ptr = (fragS *) obstack_alloc (ob, SIZEOF_STRUCT_FRAG);
  68. obstack_alignment_mask (ob) = oalign;
  69. memset (ptr, 0, SIZEOF_STRUCT_FRAG);
  70. totalfrags++;
  71. return ptr;
  72. }
  73. /* Try to augment current frag by nchars chars.
  74. If there is no room, close of the current frag with a ".fill 0"
  75. and begin a new frag. Unless the new frag has nchars chars available
  76. do not return. Do not set up any fields of *now_frag. */
  77. void
  78. frag_grow (size_t nchars)
  79. {
  80. if (obstack_room (&frchain_now->frch_obstack) < nchars)
  81. {
  82. size_t oldc;
  83. size_t newc;
  84. /* Try to allocate a bit more than needed right now. But don't do
  85. this if we would waste too much memory. Especially necessary
  86. for extremely big (like 2GB initialized) frags. */
  87. if (nchars < 0x10000)
  88. newc = 2 * nchars;
  89. else
  90. newc = nchars + 0x10000;
  91. newc += SIZEOF_STRUCT_FRAG;
  92. /* Check for possible overflow. */
  93. if (newc < nchars)
  94. as_fatal (_("can't extend frag %lu chars"), (unsigned long) nchars);
  95. /* Force to allocate at least NEWC bytes, but not less than the
  96. default. */
  97. oldc = obstack_chunk_size (&frchain_now->frch_obstack);
  98. if (newc > oldc)
  99. obstack_chunk_size (&frchain_now->frch_obstack) = newc;
  100. while (obstack_room (&frchain_now->frch_obstack) < nchars)
  101. {
  102. /* Not enough room in this frag. Close it and start a new one.
  103. This must be done in a loop because the created frag may not
  104. be big enough if the current obstack chunk is used. */
  105. frag_wane (frag_now);
  106. frag_new (0);
  107. }
  108. /* Restore the old chunk size. */
  109. obstack_chunk_size (&frchain_now->frch_obstack) = oldc;
  110. }
  111. }
  112. /* Call this to close off a completed frag, and start up a new (empty)
  113. frag, in the same subsegment as the old frag.
  114. [frchain_now remains the same but frag_now is updated.]
  115. Because this calculates the correct value of fr_fix by
  116. looking at the obstack 'frags', it needs to know how many
  117. characters at the end of the old frag belong to the maximal
  118. variable part; The rest must belong to fr_fix.
  119. It doesn't actually set up the old frag's fr_var. You may have
  120. set fr_var == 1, but allocated 10 chars to the end of the frag;
  121. In this case you pass old_frags_var_max_size == 10.
  122. In fact, you may use fr_var for something totally unrelated to the
  123. size of the variable part of the frag; None of the generic frag
  124. handling code makes use of fr_var.
  125. Make a new frag, initialising some components. Link new frag at end
  126. of frchain_now. */
  127. void
  128. frag_new (size_t old_frags_var_max_size
  129. /* Number of chars (already allocated on obstack frags) in
  130. variable_length part of frag. */)
  131. {
  132. fragS *former_last_fragP;
  133. frchainS *frchP;
  134. gas_assert (frchain_now->frch_last == frag_now);
  135. /* Fix up old frag's fr_fix. */
  136. frag_now->fr_fix = frag_now_fix_octets () - old_frags_var_max_size;
  137. /* Make sure its type is valid. */
  138. gas_assert (frag_now->fr_type != 0);
  139. /* This will align the obstack so the next struct we allocate on it
  140. will begin at a correct boundary. */
  141. obstack_finish (&frchain_now->frch_obstack);
  142. frchP = frchain_now;
  143. know (frchP);
  144. former_last_fragP = frchP->frch_last;
  145. gas_assert (former_last_fragP != 0);
  146. gas_assert (former_last_fragP == frag_now);
  147. frag_now = frag_alloc (&frchP->frch_obstack);
  148. as_where (&frag_now->fr_file, &frag_now->fr_line);
  149. /* Generally, frag_now->points to an address rounded up to next
  150. alignment. However, characters will add to obstack frags
  151. IMMEDIATELY after the struct frag, even if they are not starting
  152. at an alignment address. */
  153. former_last_fragP->fr_next = frag_now;
  154. frchP->frch_last = frag_now;
  155. #ifndef NO_LISTING
  156. {
  157. extern struct list_info_struct *listing_tail;
  158. frag_now->line = listing_tail;
  159. }
  160. #endif
  161. gas_assert (frchain_now->frch_last == frag_now);
  162. frag_now->fr_next = NULL;
  163. }
  164. /* Start a new frag unless we have n more chars of room in the current frag.
  165. Close off the old frag with a .fill 0.
  166. Return the address of the 1st char to write into. Advance
  167. frag_now_growth past the new chars. */
  168. char *
  169. frag_more (size_t nchars)
  170. {
  171. char *retval;
  172. frag_alloc_check (&frchain_now->frch_obstack);
  173. frag_grow (nchars);
  174. retval = obstack_next_free (&frchain_now->frch_obstack);
  175. obstack_blank_fast (&frchain_now->frch_obstack, nchars);
  176. return retval;
  177. }
  178. /* Close the current frag, setting its fields for a relaxable frag. Start a
  179. new frag. */
  180. static void
  181. frag_var_init (relax_stateT type, size_t max_chars, size_t var,
  182. relax_substateT subtype, symbolS *symbol, offsetT offset,
  183. char *opcode)
  184. {
  185. frag_now->fr_var = var;
  186. frag_now->fr_type = type;
  187. frag_now->fr_subtype = subtype;
  188. frag_now->fr_symbol = symbol;
  189. frag_now->fr_offset = offset;
  190. frag_now->fr_opcode = opcode;
  191. #ifdef USING_CGEN
  192. frag_now->fr_cgen.insn = 0;
  193. frag_now->fr_cgen.opindex = 0;
  194. frag_now->fr_cgen.opinfo = 0;
  195. #endif
  196. #ifdef TC_FRAG_INIT
  197. TC_FRAG_INIT (frag_now);
  198. #endif
  199. as_where (&frag_now->fr_file, &frag_now->fr_line);
  200. frag_new (max_chars);
  201. }
  202. /* Start a new frag unless we have max_chars more chars of room in the
  203. current frag. Close off the old frag with a .fill 0.
  204. Set up a machine_dependent relaxable frag, then start a new frag.
  205. Return the address of the 1st char of the var part of the old frag
  206. to write into. */
  207. char *
  208. frag_var (relax_stateT type, size_t max_chars, size_t var,
  209. relax_substateT subtype, symbolS *symbol, offsetT offset,
  210. char *opcode)
  211. {
  212. char *retval;
  213. frag_grow (max_chars);
  214. retval = obstack_next_free (&frchain_now->frch_obstack);
  215. obstack_blank_fast (&frchain_now->frch_obstack, max_chars);
  216. frag_var_init (type, max_chars, var, subtype, symbol, offset, opcode);
  217. return retval;
  218. }
  219. /* OVE: This variant of frag_var assumes that space for the tail has been
  220. allocated by caller.
  221. No call to frag_grow is done. */
  222. char *
  223. frag_variant (relax_stateT type, size_t max_chars, size_t var,
  224. relax_substateT subtype, symbolS *symbol, offsetT offset,
  225. char *opcode)
  226. {
  227. char *retval;
  228. retval = obstack_next_free (&frchain_now->frch_obstack);
  229. frag_var_init (type, max_chars, var, subtype, symbol, offset, opcode);
  230. return retval;
  231. }
  232. /* Reduce the variable end of a frag to a harmless state. */
  233. void
  234. frag_wane (fragS *fragP)
  235. {
  236. fragP->fr_type = rs_fill;
  237. fragP->fr_offset = 0;
  238. fragP->fr_var = 0;
  239. }
  240. /* Return the number of bytes by which the current frag can be grown. */
  241. size_t
  242. frag_room (void)
  243. {
  244. return obstack_room (&frchain_now->frch_obstack);
  245. }
  246. /* Make an alignment frag. The size of this frag will be adjusted to
  247. force the next frag to have the appropriate alignment. ALIGNMENT
  248. is the power of two to which to align. FILL_CHARACTER is the
  249. character to use to fill in any bytes which are skipped. MAX is
  250. the maximum number of characters to skip when doing the alignment,
  251. or 0 if there is no maximum. */
  252. void
  253. frag_align (int alignment, int fill_character, int max)
  254. {
  255. if (now_seg == absolute_section)
  256. {
  257. addressT new_off;
  258. addressT mask;
  259. mask = (~(addressT) 0) << alignment;
  260. new_off = (abs_section_offset + ~mask) & mask;
  261. if (max == 0 || new_off - abs_section_offset <= (addressT) max)
  262. abs_section_offset = new_off;
  263. }
  264. else
  265. {
  266. char *p;
  267. p = frag_var (rs_align, 1, 1, (relax_substateT) max,
  268. (symbolS *) 0, (offsetT) alignment, (char *) 0);
  269. *p = fill_character;
  270. }
  271. }
  272. /* Make an alignment frag like frag_align, but fill with a repeating
  273. pattern rather than a single byte. ALIGNMENT is the power of two
  274. to which to align. FILL_PATTERN is the fill pattern to repeat in
  275. the bytes which are skipped. N_FILL is the number of bytes in
  276. FILL_PATTERN. MAX is the maximum number of characters to skip when
  277. doing the alignment, or 0 if there is no maximum. */
  278. void
  279. frag_align_pattern (int alignment, const char *fill_pattern,
  280. size_t n_fill, int max)
  281. {
  282. char *p;
  283. p = frag_var (rs_align, n_fill, n_fill, (relax_substateT) max,
  284. (symbolS *) 0, (offsetT) alignment, (char *) 0);
  285. memcpy (p, fill_pattern, n_fill);
  286. }
  287. /* The NOP_OPCODE is for the alignment fill value. Fill it with a nop
  288. instruction so that the disassembler does not choke on it. */
  289. #ifndef NOP_OPCODE
  290. #define NOP_OPCODE 0x00
  291. #endif
  292. /* Use this to restrict the amount of memory allocated for representing
  293. the alignment code. Needs to be large enough to hold any fixed sized
  294. prologue plus the replicating portion. */
  295. #ifndef MAX_MEM_FOR_RS_ALIGN_CODE
  296. /* Assume that if HANDLE_ALIGN is not defined then no special action
  297. is required to code fill, which means that we get just repeat the
  298. one NOP_OPCODE byte. */
  299. # ifndef HANDLE_ALIGN
  300. # define MAX_MEM_FOR_RS_ALIGN_CODE 1
  301. # else
  302. # define MAX_MEM_FOR_RS_ALIGN_CODE ((1 << alignment) - 1)
  303. # endif
  304. #endif
  305. void
  306. frag_align_code (int alignment, int max)
  307. {
  308. char *p;
  309. p = frag_var (rs_align_code, MAX_MEM_FOR_RS_ALIGN_CODE, 1,
  310. (relax_substateT) max, (symbolS *) 0,
  311. (offsetT) alignment, (char *) 0);
  312. *p = NOP_OPCODE;
  313. }
  314. addressT
  315. frag_now_fix_octets (void)
  316. {
  317. if (now_seg == absolute_section)
  318. return abs_section_offset;
  319. return ((char *) obstack_next_free (&frchain_now->frch_obstack)
  320. - frag_now->fr_literal);
  321. }
  322. addressT
  323. frag_now_fix (void)
  324. {
  325. return frag_now_fix_octets () / OCTETS_PER_BYTE;
  326. }
  327. void
  328. frag_append_1_char (int datum)
  329. {
  330. frag_alloc_check (&frchain_now->frch_obstack);
  331. if (obstack_room (&frchain_now->frch_obstack) <= 1)
  332. {
  333. frag_wane (frag_now);
  334. frag_new (0);
  335. }
  336. obstack_1grow (&frchain_now->frch_obstack, datum);
  337. }
  338. /* Return TRUE if FRAG1 and FRAG2 have a fixed relationship between
  339. their start addresses. Set OFFSET to the difference in address
  340. not already accounted for in the frag FR_ADDRESS. */
  341. bfd_boolean
  342. frag_offset_fixed_p (const fragS *frag1, const fragS *frag2, offsetT *offset)
  343. {
  344. const fragS *frag;
  345. offsetT off;
  346. /* Start with offset initialised to difference between the two frags.
  347. Prior to assigning frag addresses this will be zero. */
  348. off = frag1->fr_address - frag2->fr_address;
  349. if (frag1 == frag2)
  350. {
  351. *offset = off;
  352. return TRUE;
  353. }
  354. /* Maybe frag2 is after frag1. */
  355. frag = frag1;
  356. while (frag->fr_type == rs_fill)
  357. {
  358. off += frag->fr_fix + frag->fr_offset * frag->fr_var;
  359. frag = frag->fr_next;
  360. if (frag == NULL)
  361. break;
  362. if (frag == frag2)
  363. {
  364. *offset = off;
  365. return TRUE;
  366. }
  367. }
  368. /* Maybe frag1 is after frag2. */
  369. off = frag1->fr_address - frag2->fr_address;
  370. frag = frag2;
  371. while (frag->fr_type == rs_fill)
  372. {
  373. off -= frag->fr_fix + frag->fr_offset * frag->fr_var;
  374. frag = frag->fr_next;
  375. if (frag == NULL)
  376. break;
  377. if (frag == frag1)
  378. {
  379. *offset = off;
  380. return TRUE;
  381. }
  382. }
  383. return FALSE;
  384. }