ehopt.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572
  1. /* ehopt.c--optimize gcc exception frame information.
  2. Copyright (C) 1998-2022 Free Software Foundation, Inc.
  3. Written by Ian Lance Taylor <ian@cygnus.com>.
  4. This file is part of GAS, the GNU Assembler.
  5. GAS 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, or (at your option)
  8. any later version.
  9. GAS 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 GAS; see the file COPYING. If not, write to the Free
  15. Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
  16. 02110-1301, USA. */
  17. #include "as.h"
  18. #include "subsegs.h"
  19. /* We include this ELF file, even though we may not be assembling for
  20. ELF, since the exception frame information is always in a format
  21. derived from DWARF. */
  22. #include "dwarf2.h"
  23. /* Try to optimize gcc 2.8 exception frame information.
  24. Exception frame information is emitted for every function in the
  25. .eh_frame or .debug_frame sections. Simple information for a function
  26. with no exceptions looks like this:
  27. __FRAME_BEGIN__:
  28. .4byte .LLCIE1 / Length of Common Information Entry
  29. .LSCIE1:
  30. #if .eh_frame
  31. .4byte 0x0 / CIE Identifier Tag
  32. #elif .debug_frame
  33. .4byte 0xffffffff / CIE Identifier Tag
  34. #endif
  35. .byte 0x1 / CIE Version
  36. .byte 0x0 / CIE Augmentation (none)
  37. .byte 0x1 / ULEB128 0x1 (CIE Code Alignment Factor)
  38. .byte 0x7c / SLEB128 -4 (CIE Data Alignment Factor)
  39. .byte 0x8 / CIE RA Column
  40. .byte 0xc / DW_CFA_def_cfa
  41. .byte 0x4 / ULEB128 0x4
  42. .byte 0x4 / ULEB128 0x4
  43. .byte 0x88 / DW_CFA_offset, column 0x8
  44. .byte 0x1 / ULEB128 0x1
  45. .align 4
  46. .LECIE1:
  47. .set .LLCIE1,.LECIE1-.LSCIE1 / CIE Length Symbol
  48. .4byte .LLFDE1 / FDE Length
  49. .LSFDE1:
  50. .4byte .LSFDE1-__FRAME_BEGIN__ / FDE CIE offset
  51. .4byte .LFB1 / FDE initial location
  52. .4byte .LFE1-.LFB1 / FDE address range
  53. .byte 0x4 / DW_CFA_advance_loc4
  54. .4byte .LCFI0-.LFB1
  55. .byte 0xe / DW_CFA_def_cfa_offset
  56. .byte 0x8 / ULEB128 0x8
  57. .byte 0x85 / DW_CFA_offset, column 0x5
  58. .byte 0x2 / ULEB128 0x2
  59. .byte 0x4 / DW_CFA_advance_loc4
  60. .4byte .LCFI1-.LCFI0
  61. .byte 0xd / DW_CFA_def_cfa_register
  62. .byte 0x5 / ULEB128 0x5
  63. .byte 0x4 / DW_CFA_advance_loc4
  64. .4byte .LCFI2-.LCFI1
  65. .byte 0x2e / DW_CFA_GNU_args_size
  66. .byte 0x4 / ULEB128 0x4
  67. .byte 0x4 / DW_CFA_advance_loc4
  68. .4byte .LCFI3-.LCFI2
  69. .byte 0x2e / DW_CFA_GNU_args_size
  70. .byte 0x0 / ULEB128 0x0
  71. .align 4
  72. .LEFDE1:
  73. .set .LLFDE1,.LEFDE1-.LSFDE1 / FDE Length Symbol
  74. The immediate issue we can address in the assembler is the
  75. DW_CFA_advance_loc4 followed by a four byte value. The value is
  76. the difference of two addresses in the function. Since gcc does
  77. not know this value, it always uses four bytes. We will know the
  78. value at the end of assembly, so we can do better. */
  79. struct cie_info
  80. {
  81. unsigned code_alignment;
  82. int z_augmentation;
  83. };
  84. static int get_cie_info (struct cie_info *);
  85. /* Extract information from the CIE. */
  86. static int
  87. get_cie_info (struct cie_info *info)
  88. {
  89. fragS *f;
  90. fixS *fix;
  91. unsigned int offset;
  92. char CIE_id;
  93. char augmentation[10];
  94. int iaug;
  95. int code_alignment = 0;
  96. /* We should find the CIE at the start of the section. */
  97. f = seg_info (now_seg)->frchainP->frch_root;
  98. fix = seg_info (now_seg)->frchainP->fix_root;
  99. /* Look through the frags of the section to find the code alignment. */
  100. /* First make sure that the CIE Identifier Tag is 0/-1. */
  101. if (startswith (segment_name (now_seg), ".debug_frame"))
  102. CIE_id = (char)0xff;
  103. else
  104. CIE_id = 0;
  105. offset = 4;
  106. while (f != NULL && offset >= f->fr_fix)
  107. {
  108. offset -= f->fr_fix;
  109. f = f->fr_next;
  110. }
  111. if (f == NULL
  112. || f->fr_fix - offset < 4
  113. || f->fr_literal[offset] != CIE_id
  114. || f->fr_literal[offset + 1] != CIE_id
  115. || f->fr_literal[offset + 2] != CIE_id
  116. || f->fr_literal[offset + 3] != CIE_id)
  117. return 0;
  118. /* Next make sure the CIE version number is 1. */
  119. offset += 4;
  120. while (f != NULL && offset >= f->fr_fix)
  121. {
  122. offset -= f->fr_fix;
  123. f = f->fr_next;
  124. }
  125. if (f == NULL
  126. || f->fr_fix - offset < 1
  127. || f->fr_literal[offset] != 1)
  128. return 0;
  129. /* Skip the augmentation (a null terminated string). */
  130. iaug = 0;
  131. ++offset;
  132. while (1)
  133. {
  134. while (f != NULL && offset >= f->fr_fix)
  135. {
  136. offset -= f->fr_fix;
  137. f = f->fr_next;
  138. }
  139. if (f == NULL)
  140. return 0;
  141. while (offset < f->fr_fix && f->fr_literal[offset] != '\0')
  142. {
  143. if ((size_t) iaug < (sizeof augmentation) - 1)
  144. {
  145. augmentation[iaug] = f->fr_literal[offset];
  146. ++iaug;
  147. }
  148. ++offset;
  149. }
  150. if (offset < f->fr_fix)
  151. break;
  152. }
  153. ++offset;
  154. while (f != NULL && offset >= f->fr_fix)
  155. {
  156. offset -= f->fr_fix;
  157. f = f->fr_next;
  158. }
  159. if (f == NULL)
  160. return 0;
  161. augmentation[iaug] = '\0';
  162. if (augmentation[0] == '\0')
  163. {
  164. /* No augmentation. */
  165. }
  166. else if (strcmp (augmentation, "eh") == 0)
  167. {
  168. /* We have to skip a pointer. Unfortunately, we don't know how
  169. large it is. We find out by looking for a matching fixup. */
  170. while (fix != NULL
  171. && (fix->fx_frag != f || fix->fx_where != offset))
  172. fix = fix->fx_next;
  173. if (fix == NULL)
  174. offset += 4;
  175. else
  176. offset += fix->fx_size;
  177. while (f != NULL && offset >= f->fr_fix)
  178. {
  179. offset -= f->fr_fix;
  180. f = f->fr_next;
  181. }
  182. if (f == NULL)
  183. return 0;
  184. }
  185. else if (augmentation[0] != 'z')
  186. return 0;
  187. /* We're now at the code alignment factor, which is a ULEB128. If
  188. it isn't a single byte, forget it. */
  189. code_alignment = f->fr_literal[offset] & 0xff;
  190. if ((code_alignment & 0x80) != 0)
  191. code_alignment = 0;
  192. info->code_alignment = code_alignment;
  193. info->z_augmentation = (augmentation[0] == 'z');
  194. return 1;
  195. }
  196. enum frame_state
  197. {
  198. state_idle,
  199. state_saw_size,
  200. state_saw_cie_offset,
  201. state_saw_pc_begin,
  202. state_seeing_aug_size,
  203. state_skipping_aug,
  204. state_wait_loc4,
  205. state_saw_loc4,
  206. state_error,
  207. };
  208. /* This function is called from emit_expr. It looks for cases which
  209. we can optimize.
  210. Rather than try to parse all this information as we read it, we
  211. look for a single byte DW_CFA_advance_loc4 followed by a 4 byte
  212. difference. We turn that into a rs_cfa_advance frag, and handle
  213. those frags at the end of the assembly. If the gcc output changes
  214. somewhat, this optimization may stop working.
  215. This function returns non-zero if it handled the expression and
  216. emit_expr should not do anything, or zero otherwise. It can also
  217. change *EXP and *PNBYTES. */
  218. int
  219. check_eh_frame (expressionS *exp, unsigned int *pnbytes)
  220. {
  221. struct frame_data
  222. {
  223. enum frame_state state;
  224. int cie_info_ok;
  225. struct cie_info cie_info;
  226. symbolS *size_end_sym;
  227. fragS *loc4_frag;
  228. int loc4_fix;
  229. int aug_size;
  230. int aug_shift;
  231. };
  232. static struct frame_data eh_frame_data;
  233. static struct frame_data debug_frame_data;
  234. struct frame_data *d;
  235. /* Don't optimize. */
  236. if (flag_traditional_format)
  237. return 0;
  238. #ifdef md_allow_eh_opt
  239. if (! md_allow_eh_opt)
  240. return 0;
  241. #endif
  242. /* Select the proper section data. */
  243. if (startswith (segment_name (now_seg), ".eh_frame")
  244. && segment_name (now_seg)[9] != '_')
  245. d = &eh_frame_data;
  246. else if (startswith (segment_name (now_seg), ".debug_frame"))
  247. d = &debug_frame_data;
  248. else
  249. return 0;
  250. if (d->state >= state_saw_size && S_IS_DEFINED (d->size_end_sym))
  251. {
  252. /* We have come to the end of the CIE or FDE. See below where
  253. we set saw_size. We must check this first because we may now
  254. be looking at the next size. */
  255. d->state = state_idle;
  256. }
  257. switch (d->state)
  258. {
  259. case state_idle:
  260. if (*pnbytes == 4)
  261. {
  262. /* This might be the size of the CIE or FDE. We want to know
  263. the size so that we don't accidentally optimize across an FDE
  264. boundary. We recognize the size in one of two forms: a
  265. symbol which will later be defined as a difference, or a
  266. subtraction of two symbols. Either way, we can tell when we
  267. are at the end of the FDE because the symbol becomes defined
  268. (in the case of a subtraction, the end symbol, from which the
  269. start symbol is being subtracted). Other ways of describing
  270. the size will not be optimized. */
  271. if ((exp->X_op == O_symbol || exp->X_op == O_subtract)
  272. && ! S_IS_DEFINED (exp->X_add_symbol))
  273. {
  274. d->state = state_saw_size;
  275. d->size_end_sym = exp->X_add_symbol;
  276. }
  277. }
  278. break;
  279. case state_saw_size:
  280. case state_saw_cie_offset:
  281. /* Assume whatever form it appears in, it appears atomically. */
  282. d->state = (enum frame_state) (d->state + 1);
  283. break;
  284. case state_saw_pc_begin:
  285. /* Decide whether we should see an augmentation. */
  286. if (! d->cie_info_ok
  287. && ! (d->cie_info_ok = get_cie_info (&d->cie_info)))
  288. d->state = state_error;
  289. else if (d->cie_info.z_augmentation)
  290. {
  291. d->state = state_seeing_aug_size;
  292. d->aug_size = 0;
  293. d->aug_shift = 0;
  294. }
  295. else
  296. d->state = state_wait_loc4;
  297. break;
  298. case state_seeing_aug_size:
  299. /* Bytes == -1 means this comes from an leb128 directive. */
  300. if ((int)*pnbytes == -1 && exp->X_op == O_constant)
  301. {
  302. d->aug_size = exp->X_add_number;
  303. d->state = state_skipping_aug;
  304. }
  305. else if (*pnbytes == 1 && exp->X_op == O_constant)
  306. {
  307. unsigned char byte = exp->X_add_number;
  308. d->aug_size |= (byte & 0x7f) << d->aug_shift;
  309. d->aug_shift += 7;
  310. if ((byte & 0x80) == 0)
  311. d->state = state_skipping_aug;
  312. }
  313. else
  314. d->state = state_error;
  315. if (d->state == state_skipping_aug && d->aug_size == 0)
  316. d->state = state_wait_loc4;
  317. break;
  318. case state_skipping_aug:
  319. if ((int)*pnbytes < 0)
  320. d->state = state_error;
  321. else
  322. {
  323. int left = (d->aug_size -= *pnbytes);
  324. if (left == 0)
  325. d->state = state_wait_loc4;
  326. else if (left < 0)
  327. d->state = state_error;
  328. }
  329. break;
  330. case state_wait_loc4:
  331. if (*pnbytes == 1
  332. && exp->X_op == O_constant
  333. && exp->X_add_number == DW_CFA_advance_loc4)
  334. {
  335. /* This might be a DW_CFA_advance_loc4. Record the frag and the
  336. position within the frag, so that we can change it later. */
  337. frag_grow (1);
  338. d->state = state_saw_loc4;
  339. d->loc4_frag = frag_now;
  340. d->loc4_fix = frag_now_fix ();
  341. }
  342. break;
  343. case state_saw_loc4:
  344. d->state = state_wait_loc4;
  345. if (*pnbytes != 4)
  346. break;
  347. if (exp->X_op == O_constant)
  348. {
  349. /* This is a case which we can optimize. The two symbols being
  350. subtracted were in the same frag and the expression was
  351. reduced to a constant. We can do the optimization entirely
  352. in this function. */
  353. if (exp->X_add_number < 0x40)
  354. {
  355. d->loc4_frag->fr_literal[d->loc4_fix]
  356. = DW_CFA_advance_loc | exp->X_add_number;
  357. /* No more bytes needed. */
  358. return 1;
  359. }
  360. else if (exp->X_add_number < 0x100)
  361. {
  362. d->loc4_frag->fr_literal[d->loc4_fix] = DW_CFA_advance_loc1;
  363. *pnbytes = 1;
  364. }
  365. else if (exp->X_add_number < 0x10000)
  366. {
  367. d->loc4_frag->fr_literal[d->loc4_fix] = DW_CFA_advance_loc2;
  368. *pnbytes = 2;
  369. }
  370. }
  371. else if (exp->X_op == O_subtract && d->cie_info.code_alignment == 1)
  372. {
  373. /* This is a case we can optimize. The expression was not
  374. reduced, so we can not finish the optimization until the end
  375. of the assembly. We set up a variant frag which we handle
  376. later. */
  377. frag_var (rs_cfa, 4, 0, 1 << 3, make_expr_symbol (exp),
  378. d->loc4_fix, (char *) d->loc4_frag);
  379. return 1;
  380. }
  381. else if ((exp->X_op == O_divide
  382. || exp->X_op == O_right_shift)
  383. && d->cie_info.code_alignment > 1)
  384. {
  385. if (symbol_symbolS (exp->X_add_symbol)
  386. && symbol_constant_p (exp->X_op_symbol)
  387. && S_GET_SEGMENT (exp->X_op_symbol) == absolute_section
  388. && ((exp->X_op == O_divide
  389. ? *symbol_X_add_number (exp->X_op_symbol)
  390. : (offsetT) 1 << *symbol_X_add_number (exp->X_op_symbol))
  391. == (offsetT) d->cie_info.code_alignment))
  392. {
  393. expressionS *symval;
  394. symval = symbol_get_value_expression (exp->X_add_symbol);
  395. if (symval->X_op == O_subtract)
  396. {
  397. /* This is a case we can optimize as well. The
  398. expression was not reduced, so we can not finish
  399. the optimization until the end of the assembly.
  400. We set up a variant frag which we handle later. */
  401. frag_var (rs_cfa, 4, 0, d->cie_info.code_alignment << 3,
  402. make_expr_symbol (symval),
  403. d->loc4_fix, (char *) d->loc4_frag);
  404. return 1;
  405. }
  406. }
  407. }
  408. break;
  409. case state_error:
  410. /* Just skipping everything. */
  411. break;
  412. }
  413. return 0;
  414. }
  415. /* The function estimates the size of a rs_cfa variant frag based on
  416. the current values of the symbols. It is called before the
  417. relaxation loop. We set fr_subtype{0:2} to the expected length. */
  418. int
  419. eh_frame_estimate_size_before_relax (fragS *frag)
  420. {
  421. offsetT diff;
  422. int ca = frag->fr_subtype >> 3;
  423. int ret;
  424. diff = resolve_symbol_value (frag->fr_symbol);
  425. gas_assert (ca > 0);
  426. diff /= ca;
  427. if (diff == 0)
  428. ret = -1;
  429. else if (diff < 0x40)
  430. ret = 0;
  431. else if (diff < 0x100)
  432. ret = 1;
  433. else if (diff < 0x10000)
  434. ret = 2;
  435. else
  436. ret = 4;
  437. frag->fr_subtype = (frag->fr_subtype & ~7) | (ret & 7);
  438. return ret;
  439. }
  440. /* This function relaxes a rs_cfa variant frag based on the current
  441. values of the symbols. fr_subtype{0:2} is the current length of
  442. the frag. This returns the change in frag length. */
  443. int
  444. eh_frame_relax_frag (fragS *frag)
  445. {
  446. int oldsize, newsize;
  447. oldsize = frag->fr_subtype & 7;
  448. if (oldsize == 7)
  449. oldsize = -1;
  450. newsize = eh_frame_estimate_size_before_relax (frag);
  451. return newsize - oldsize;
  452. }
  453. /* This function converts a rs_cfa variant frag into a normal fill
  454. frag. This is called after all relaxation has been done.
  455. fr_subtype{0:2} will be the desired length of the frag. */
  456. void
  457. eh_frame_convert_frag (fragS *frag)
  458. {
  459. offsetT diff;
  460. fragS *loc4_frag;
  461. int loc4_fix, ca;
  462. loc4_frag = (fragS *) frag->fr_opcode;
  463. loc4_fix = (int) frag->fr_offset;
  464. diff = resolve_symbol_value (frag->fr_symbol);
  465. ca = frag->fr_subtype >> 3;
  466. gas_assert (ca > 0);
  467. diff /= ca;
  468. switch (frag->fr_subtype & 7)
  469. {
  470. case 0:
  471. gas_assert (diff < 0x40);
  472. loc4_frag->fr_literal[loc4_fix] = DW_CFA_advance_loc | diff;
  473. break;
  474. case 1:
  475. gas_assert (diff < 0x100);
  476. loc4_frag->fr_literal[loc4_fix] = DW_CFA_advance_loc1;
  477. frag->fr_literal[frag->fr_fix] = diff;
  478. break;
  479. case 2:
  480. gas_assert (diff < 0x10000);
  481. loc4_frag->fr_literal[loc4_fix] = DW_CFA_advance_loc2;
  482. md_number_to_chars (frag->fr_literal + frag->fr_fix, diff, 2);
  483. break;
  484. case 4:
  485. md_number_to_chars (frag->fr_literal + frag->fr_fix, diff, 4);
  486. break;
  487. case 7:
  488. gas_assert (diff == 0);
  489. frag->fr_fix -= 8;
  490. break;
  491. default:
  492. abort ();
  493. }
  494. frag->fr_fix += frag->fr_subtype & 7;
  495. frag->fr_type = rs_fill;
  496. frag->fr_subtype = 0;
  497. frag->fr_offset = 0;
  498. }