aboutsummaryrefslogtreecommitdiff
path: root/gas/sframe-opt.c
blob: ec0509f89779f6f62f2b7544ba961e7f09d4cd11 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/* sframe-opt.c - optimize FRE and FDE information in SFrame.
   Copyright (C) 2022-2023 Free Software Foundation, Inc.

   This file is part of GAS, the GNU Assembler.

   GAS is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3, or (at your option)
   any later version.

   GAS is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with GAS; see the file COPYING.  If not, write to the Free
   Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
   02110-1301, USA.  */

#include "as.h"
#include "sframe.h"

/* The function estimates the size of a rs_sframe variant frag based on
   the current values of the symbols.  It is called before the
   relaxation loop.  We set fr_subtype{0:2} to the expected length.  */

int
sframe_estimate_size_before_relax (fragS *frag)
{
  offsetT width;
  expressionS *exp;
  symbolS *widthS;
  int ret;

  /* We are dealing with two different kind of fragments here which need
     to be fixed up:
       - first, FRE start address in each FRE, and
       - second, Function info in each FDE (function info stores the FRE type)
     The two kind of fragments can be differentiated based on the opcode
     of the symbol.  */
  exp = symbol_get_value_expression (frag->fr_symbol);
  gas_assert ((exp->X_op == O_modulus) || (exp->X_op == O_absent));
  /* Fragment for function info in an SFrame FDE will always write
     only one byte.  */
  if (exp->X_op == O_modulus)
    ret = 1;
  /* Fragment for the start address in an SFrame FRE may write out
     1/2/4 bytes depending on the value of the diff.  */
  else
    {
      /* Get the width expression from the symbol.  */
      widthS = exp->X_op_symbol;
      width = resolve_symbol_value (widthS);

      if (width < (offsetT) SFRAME_FRE_TYPE_ADDR1_LIMIT)
	ret = 1;
      else if (width < (offsetT) SFRAME_FRE_TYPE_ADDR2_LIMIT)
	ret = 2;
      else
	ret = 4;
    }

  frag->fr_subtype = (frag->fr_subtype & ~7) | (ret & 7);

  return ret;
}

/* This function relaxes a rs_sframe variant frag based on the current
   values of the symbols.  fr_subtype{0:2} is the current length of
   the frag.  This returns the change in frag length.  */

int
sframe_relax_frag (fragS *frag)
{
  int oldsize, newsize;

  oldsize = frag->fr_subtype & 7;
  if (oldsize == 7)
    oldsize = -1;
  newsize = sframe_estimate_size_before_relax (frag);
  return newsize - oldsize;
}

/* This function converts a rs_sframe variant frag into a normal fill
   frag.  This is called after all relaxation has been done.
   fr_subtype{0:2} will be the desired length of the frag.  */

void
sframe_convert_frag (fragS *frag)
{
  offsetT fsize;
  offsetT diff;
  offsetT value;

  offsetT rest_of_data;
  uint8_t fde_type, fre_type;
  uint8_t pauth_key;

  expressionS *exp;
  symbolS *dataS;
  symbolS *fsizeS, *diffS;

  /* We are dealing with two different kind of fragments here which need
     to be fixed up:
       - first, FRE start address in each FRE, and
       - second, Function info in each FDE (function info stores the FRE type)
     The two kind of fragments can be differentiated based on the opcode
     of the symbol.  */
  exp = symbol_get_value_expression (frag->fr_symbol);
  gas_assert ((exp->X_op == O_modulus) || (exp->X_op == O_absent));
  /* Fragment for function info in an SFrame FDE.  */
  if (exp->X_op == O_modulus)
    {
      /* Gather the existing value of the rest of the data except
	 the fre_type.  */
      dataS = exp->X_add_symbol;
      rest_of_data = (symbol_get_value_expression(dataS))->X_add_number;
      fde_type = SFRAME_V1_FUNC_FDE_TYPE (rest_of_data);
      pauth_key = SFRAME_V1_FUNC_PAUTH_KEY (rest_of_data);
      gas_assert (fde_type == SFRAME_FDE_TYPE_PCINC);

      /* Calculate the applicable fre_type.  */
      fsizeS = exp->X_op_symbol;
      fsize = resolve_symbol_value (fsizeS);
      if (fsize < (offsetT) SFRAME_FRE_TYPE_ADDR1_LIMIT)
	fre_type = SFRAME_FRE_TYPE_ADDR1;
      else if (fsize < (offsetT) SFRAME_FRE_TYPE_ADDR2_LIMIT)
	fre_type = SFRAME_FRE_TYPE_ADDR2;
      else
	fre_type = SFRAME_FRE_TYPE_ADDR4;

      /* Create the new function info.  */
      value = SFRAME_V1_FUNC_INFO (fde_type, fre_type);
      value = SFRAME_V1_FUNC_INFO_UPDATE_PAUTH_KEY (pauth_key, value);

      frag->fr_literal[frag->fr_fix] = value;
    }
  /* Fragment for the start address in an SFrame FRE.  */
  else
    {
      /* Get the fsize expression from the symbol.  */
      fsizeS = exp->X_op_symbol;
      fsize = resolve_symbol_value (fsizeS);
      /* Get the diff expression from the symbol.  */
      diffS= exp->X_add_symbol;
      diff = resolve_symbol_value (diffS);
      value = diff;

      switch (frag->fr_subtype & 7)
	{
	case 1:
	  gas_assert (fsize < (offsetT) SFRAME_FRE_TYPE_ADDR1_LIMIT);
	  frag->fr_literal[frag->fr_fix] = diff;
	  break;
	case 2:
	  gas_assert (fsize < (offsetT) SFRAME_FRE_TYPE_ADDR2_LIMIT);
	  md_number_to_chars (frag->fr_literal + frag->fr_fix, diff, 2);
	  break;
	case 4:
	  md_number_to_chars (frag->fr_literal + frag->fr_fix, diff, 4);
	  break;
	default:
	  abort ();
	}
    }

  frag->fr_fix += frag->fr_subtype & 7;
  frag->fr_type = rs_fill;
  frag->fr_subtype = 0;
  frag->fr_offset = 0;
  /* FIXME do this now because we have evaluated and fixed up the fragments
     manually ?  */
  frag->fr_symbol = 0;
}