frags.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566
  1. /* frags.c - manage frags -
  2. Copyright (C) 1987-2022 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 off the current frag with a ".fill 0"
  75. and begin a new frag. Then loop until the new frag has at least
  76. nchars chars available. Does not set up any fields in frag_now. */
  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 (ngettext ("can't extend frag %lu char",
  95. "can't extend frag %lu chars",
  96. (unsigned long) nchars),
  97. (unsigned long) nchars);
  98. /* Force to allocate at least NEWC bytes, but not less than the
  99. default. */
  100. oldc = obstack_chunk_size (&frchain_now->frch_obstack);
  101. if (newc > oldc)
  102. obstack_chunk_size (&frchain_now->frch_obstack) = newc;
  103. while (obstack_room (&frchain_now->frch_obstack) < nchars)
  104. {
  105. /* Not enough room in this frag. Close it and start a new one.
  106. This must be done in a loop because the created frag may not
  107. be big enough if the current obstack chunk is used. */
  108. frag_wane (frag_now);
  109. frag_new (0);
  110. }
  111. /* Restore the old chunk size. */
  112. obstack_chunk_size (&frchain_now->frch_obstack) = oldc;
  113. }
  114. }
  115. /* Call this to close off a completed frag, and start up a new (empty)
  116. frag, in the same subsegment as the old frag.
  117. [frchain_now remains the same but frag_now is updated.]
  118. Because this calculates the correct value of fr_fix by
  119. looking at the obstack 'frags', it needs to know how many
  120. characters at the end of the old frag belong to the maximal
  121. variable part; The rest must belong to fr_fix.
  122. It doesn't actually set up the old frag's fr_var. You may have
  123. set fr_var == 1, but allocated 10 chars to the end of the frag;
  124. In this case you pass old_frags_var_max_size == 10.
  125. In fact, you may use fr_var for something totally unrelated to the
  126. size of the variable part of the frag; None of the generic frag
  127. handling code makes use of fr_var.
  128. Make a new frag, initialising some components. Link new frag at end
  129. of frchain_now. */
  130. void
  131. frag_new (size_t old_frags_var_max_size
  132. /* Number of chars (already allocated on obstack frags) in
  133. variable_length part of frag. */)
  134. {
  135. fragS *former_last_fragP;
  136. frchainS *frchP;
  137. gas_assert (frchain_now->frch_last == frag_now);
  138. /* Fix up old frag's fr_fix. */
  139. frag_now->fr_fix = frag_now_fix_octets ();
  140. gas_assert (frag_now->fr_fix >= old_frags_var_max_size);
  141. frag_now->fr_fix -= old_frags_var_max_size;
  142. /* Make sure its type is valid. */
  143. gas_assert (frag_now->fr_type != 0);
  144. /* This will align the obstack so the next struct we allocate on it
  145. will begin at a correct boundary. */
  146. obstack_finish (&frchain_now->frch_obstack);
  147. frchP = frchain_now;
  148. know (frchP);
  149. former_last_fragP = frchP->frch_last;
  150. gas_assert (former_last_fragP != 0);
  151. gas_assert (former_last_fragP == frag_now);
  152. frag_now = frag_alloc (&frchP->frch_obstack);
  153. frag_now->fr_file = as_where (&frag_now->fr_line);
  154. /* Generally, frag_now->points to an address rounded up to next
  155. alignment. However, characters will add to obstack frags
  156. IMMEDIATELY after the struct frag, even if they are not starting
  157. at an alignment address. */
  158. former_last_fragP->fr_next = frag_now;
  159. frchP->frch_last = frag_now;
  160. #ifndef NO_LISTING
  161. {
  162. extern struct list_info_struct *listing_tail;
  163. frag_now->line = listing_tail;
  164. }
  165. #endif
  166. gas_assert (frchain_now->frch_last == frag_now);
  167. frag_now->fr_next = NULL;
  168. }
  169. /* Start a new frag unless we have n more chars of room in the current frag.
  170. Close off the old frag with a .fill 0.
  171. Return the address of the 1st char to write into. Advance
  172. frag_now_growth past the new chars. */
  173. char *
  174. frag_more (size_t nchars)
  175. {
  176. char *retval;
  177. frag_alloc_check (&frchain_now->frch_obstack);
  178. frag_grow (nchars);
  179. retval = obstack_next_free (&frchain_now->frch_obstack);
  180. obstack_blank_fast (&frchain_now->frch_obstack, nchars);
  181. return retval;
  182. }
  183. /* Close the current frag, setting its fields for a relaxable frag. Start a
  184. new frag. */
  185. static void
  186. frag_var_init (relax_stateT type, size_t max_chars, size_t var,
  187. relax_substateT subtype, symbolS *symbol, offsetT offset,
  188. char *opcode)
  189. {
  190. frag_now->fr_var = var;
  191. frag_now->fr_type = type;
  192. frag_now->fr_subtype = subtype;
  193. frag_now->fr_symbol = symbol;
  194. frag_now->fr_offset = offset;
  195. frag_now->fr_opcode = opcode;
  196. #ifdef USING_CGEN
  197. frag_now->fr_cgen.insn = 0;
  198. frag_now->fr_cgen.opindex = 0;
  199. frag_now->fr_cgen.opinfo = 0;
  200. #endif
  201. #ifdef TC_FRAG_INIT
  202. TC_FRAG_INIT (frag_now, max_chars);
  203. #endif
  204. frag_now->fr_file = as_where (&frag_now->fr_line);
  205. frag_new (max_chars);
  206. }
  207. /* Start a new frag unless we have max_chars more chars of room in the
  208. current frag. Close off the old frag with a .fill 0.
  209. Set up a machine_dependent relaxable frag, then start a new frag.
  210. Return the address of the 1st char of the var part of the old frag
  211. to write into. */
  212. char *
  213. frag_var (relax_stateT type, size_t max_chars, size_t var,
  214. relax_substateT subtype, symbolS *symbol, offsetT offset,
  215. char *opcode)
  216. {
  217. char *retval;
  218. frag_grow (max_chars);
  219. retval = obstack_next_free (&frchain_now->frch_obstack);
  220. obstack_blank_fast (&frchain_now->frch_obstack, max_chars);
  221. frag_var_init (type, max_chars, var, subtype, symbol, offset, opcode);
  222. return retval;
  223. }
  224. /* OVE: This variant of frag_var assumes that space for the tail has been
  225. allocated by caller.
  226. No call to frag_grow is done. */
  227. char *
  228. frag_variant (relax_stateT type, size_t max_chars, size_t var,
  229. relax_substateT subtype, symbolS *symbol, offsetT offset,
  230. char *opcode)
  231. {
  232. char *retval;
  233. retval = obstack_next_free (&frchain_now->frch_obstack);
  234. frag_var_init (type, max_chars, var, subtype, symbol, offset, opcode);
  235. return retval;
  236. }
  237. /* Reduce the variable end of a frag to a harmless state. */
  238. void
  239. frag_wane (fragS *fragP)
  240. {
  241. fragP->fr_type = rs_fill;
  242. fragP->fr_offset = 0;
  243. fragP->fr_var = 0;
  244. }
  245. /* Return the number of bytes by which the current frag can be grown. */
  246. size_t
  247. frag_room (void)
  248. {
  249. return obstack_room (&frchain_now->frch_obstack);
  250. }
  251. /* Make an alignment frag. The size of this frag will be adjusted to
  252. force the next frag to have the appropriate alignment. ALIGNMENT
  253. is the power of two to which to align. FILL_CHARACTER is the
  254. character to use to fill in any bytes which are skipped. MAX is
  255. the maximum number of characters to skip when doing the alignment,
  256. or 0 if there is no maximum. */
  257. void
  258. frag_align (int alignment, int fill_character, int max)
  259. {
  260. if (now_seg == absolute_section)
  261. {
  262. addressT new_off;
  263. addressT mask;
  264. mask = (~(addressT) 0) << alignment;
  265. new_off = (abs_section_offset + ~mask) & mask;
  266. if (max == 0 || new_off - abs_section_offset <= (addressT) max)
  267. abs_section_offset = new_off;
  268. }
  269. else
  270. {
  271. char *p;
  272. p = frag_var (rs_align, 1, 1, (relax_substateT) max,
  273. (symbolS *) 0, (offsetT) alignment, (char *) 0);
  274. *p = fill_character;
  275. }
  276. }
  277. /* Make an alignment frag like frag_align, but fill with a repeating
  278. pattern rather than a single byte. ALIGNMENT is the power of two
  279. to which to align. FILL_PATTERN is the fill pattern to repeat in
  280. the bytes which are skipped. N_FILL is the number of bytes in
  281. FILL_PATTERN. MAX is the maximum number of characters to skip when
  282. doing the alignment, or 0 if there is no maximum. */
  283. void
  284. frag_align_pattern (int alignment, const char *fill_pattern,
  285. size_t n_fill, int max)
  286. {
  287. char *p;
  288. p = frag_var (rs_align, n_fill, n_fill, (relax_substateT) max,
  289. (symbolS *) 0, (offsetT) alignment, (char *) 0);
  290. memcpy (p, fill_pattern, n_fill);
  291. }
  292. /* The NOP_OPCODE is for the alignment fill value. Fill it with a nop
  293. instruction so that the disassembler does not choke on it. */
  294. #ifndef NOP_OPCODE
  295. #define NOP_OPCODE 0x00
  296. #endif
  297. /* Use this to restrict the amount of memory allocated for representing
  298. the alignment code. Needs to be large enough to hold any fixed sized
  299. prologue plus the replicating portion. */
  300. #ifndef MAX_MEM_FOR_RS_ALIGN_CODE
  301. /* Assume that if HANDLE_ALIGN is not defined then no special action
  302. is required to code fill, which means that we get just repeat the
  303. one NOP_OPCODE byte. */
  304. # ifndef HANDLE_ALIGN
  305. # define MAX_MEM_FOR_RS_ALIGN_CODE 1
  306. # else
  307. # define MAX_MEM_FOR_RS_ALIGN_CODE ((1 << alignment) - 1)
  308. # endif
  309. #endif
  310. void
  311. frag_align_code (int alignment, int max)
  312. {
  313. char *p;
  314. p = frag_var (rs_align_code, MAX_MEM_FOR_RS_ALIGN_CODE, 1,
  315. (relax_substateT) max, (symbolS *) 0,
  316. (offsetT) alignment, (char *) 0);
  317. *p = NOP_OPCODE;
  318. }
  319. addressT
  320. frag_now_fix_octets (void)
  321. {
  322. if (now_seg == absolute_section)
  323. return abs_section_offset;
  324. return ((char *) obstack_next_free (&frchain_now->frch_obstack)
  325. - frag_now->fr_literal);
  326. }
  327. addressT
  328. frag_now_fix (void)
  329. {
  330. /* Symbols whose section has SEC_ELF_OCTETS set,
  331. resolve to octets instead of target bytes. */
  332. if (now_seg->flags & SEC_OCTETS)
  333. return frag_now_fix_octets ();
  334. else
  335. return frag_now_fix_octets () / OCTETS_PER_BYTE;
  336. }
  337. void
  338. frag_append_1_char (int datum)
  339. {
  340. frag_alloc_check (&frchain_now->frch_obstack);
  341. if (obstack_room (&frchain_now->frch_obstack) <= 1)
  342. {
  343. frag_wane (frag_now);
  344. frag_new (0);
  345. }
  346. obstack_1grow (&frchain_now->frch_obstack, datum);
  347. }
  348. /* Return TRUE if FRAG1 and FRAG2 have a fixed relationship between
  349. their start addresses. Set OFFSET to the difference in address
  350. not already accounted for in the frag FR_ADDRESS. */
  351. bool
  352. frag_offset_fixed_p (const fragS *frag1, const fragS *frag2, offsetT *offset)
  353. {
  354. const fragS *frag;
  355. offsetT off;
  356. /* Start with offset initialised to difference between the two frags.
  357. Prior to assigning frag addresses this will be zero. */
  358. off = frag1->fr_address - frag2->fr_address;
  359. if (frag1 == frag2)
  360. {
  361. *offset = off;
  362. return true;
  363. }
  364. /* Maybe frag2 is after frag1. */
  365. frag = frag1;
  366. while (frag->fr_type == rs_fill)
  367. {
  368. off += frag->fr_fix + frag->fr_offset * frag->fr_var;
  369. frag = frag->fr_next;
  370. if (frag == NULL)
  371. break;
  372. if (frag == frag2)
  373. {
  374. *offset = off;
  375. return true;
  376. }
  377. }
  378. /* Maybe frag1 is after frag2. */
  379. off = frag1->fr_address - frag2->fr_address;
  380. frag = frag2;
  381. while (frag->fr_type == rs_fill)
  382. {
  383. off -= frag->fr_fix + frag->fr_offset * frag->fr_var;
  384. frag = frag->fr_next;
  385. if (frag == NULL)
  386. break;
  387. if (frag == frag1)
  388. {
  389. *offset = off;
  390. return true;
  391. }
  392. }
  393. return false;
  394. }
  395. /* Return TRUE if FRAG2 follows FRAG1 with a fixed relationship
  396. between the two assuming alignment frags do nothing. Set OFFSET to
  397. the difference in address not already accounted for in the frag
  398. FR_ADDRESS. */
  399. bool
  400. frag_offset_ignore_align_p (const fragS *frag1, const fragS *frag2,
  401. offsetT *offset)
  402. {
  403. const fragS *frag;
  404. offsetT off;
  405. /* Start with offset initialised to difference between the two frags.
  406. Prior to assigning frag addresses this will be zero. */
  407. off = frag1->fr_address - frag2->fr_address;
  408. if (frag1 == frag2)
  409. {
  410. *offset = off;
  411. return true;
  412. }
  413. frag = frag1;
  414. while (frag->fr_type == rs_fill
  415. || frag->fr_type == rs_align
  416. || frag->fr_type == rs_align_code
  417. || frag->fr_type == rs_align_test)
  418. {
  419. if (frag->fr_type == rs_fill)
  420. off += frag->fr_fix + frag->fr_offset * frag->fr_var;
  421. frag = frag->fr_next;
  422. if (frag == NULL)
  423. break;
  424. if (frag == frag2)
  425. {
  426. *offset = off;
  427. return true;
  428. }
  429. }
  430. return false;
  431. }
  432. /* Return TRUE if we can determine whether FRAG2 OFF2 appears after
  433. (strict >, not >=) FRAG1 OFF1, assuming it is not before. Set
  434. *OFFSET so that resolve_expression will resolve an O_gt operation
  435. between them to false (0) if they are guaranteed to be at the same
  436. location, or to true (-1) if they are guaranteed to be at different
  437. locations. Return FALSE conservatively, e.g. if neither result can
  438. be guaranteed (yet).
  439. They are known to be in the same segment, and not the same frag
  440. (this is a fallback for frag_offset_fixed_p, that always takes care
  441. of this case), and it is expected (from the uses this is designed
  442. to simplify, namely location view increments) that frag2 is
  443. reachable from frag1 following the fr_next links, rather than the
  444. other way round. */
  445. bool
  446. frag_gtoffset_p (valueT off2, const fragS *frag2,
  447. valueT off1, const fragS *frag1, offsetT *offset)
  448. {
  449. /* Insanity check. */
  450. if (frag2 == frag1 || off1 > frag1->fr_fix)
  451. return false;
  452. /* If the first symbol offset is at the end of the first frag and
  453. the second symbol offset at the beginning of the second frag then
  454. it is possible they are at the same address. Go looking for a
  455. non-zero fr_fix in any frag between these frags. If found then
  456. we can say the O_gt result will be true. If no such frag is
  457. found we assume that frag1 or any of the following frags might
  458. have a variable tail and thus the answer is unknown. This isn't
  459. strictly true; some frags don't have a variable tail, but it
  460. doesn't seem worth optimizing for those cases. */
  461. const fragS *frag = frag1;
  462. offsetT delta = off2 - off1;
  463. for (;;)
  464. {
  465. delta += frag->fr_fix;
  466. frag = frag->fr_next;
  467. if (frag == frag2)
  468. {
  469. if (delta == 0)
  470. return false;
  471. break;
  472. }
  473. /* If we run off the end of the frag chain then we have a case
  474. where frag2 is not after frag1, ie. an O_gt expression not
  475. created for .loc view. */
  476. if (frag == NULL)
  477. return false;
  478. }
  479. *offset = (off2 - off1 - delta) * OCTETS_PER_BYTE;
  480. return true;
  481. }