aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/tools/yasm/libyasm/bc-align.c
blob: 42a9cc4671e4f2223c4f76d67064db3aff5072be (plain) (blame)
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
/* 
 * Align bytecode 
 * 
 *  Copyright (C) 2005-2007  Peter Johnson 
 * 
 * Redistribution and use in source and binary forms, with or without 
 * modification, are permitted provided that the following conditions 
 * are met: 
 * 1. Redistributions of source code must retain the above copyright 
 *    notice, this list of conditions and the following disclaimer. 
 * 2. Redistributions in binary form must reproduce the above copyright 
 *    notice, this list of conditions and the following disclaimer in the 
 *    documentation and/or other materials provided with the distribution. 
 * 
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND OTHER CONTRIBUTORS ``AS IS'' 
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR OTHER CONTRIBUTORS BE 
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
 * POSSIBILITY OF SUCH DAMAGE. 
 */ 
#include "util.h" 
 
#include "libyasm-stdint.h" 
#include "coretype.h" 
 
#include "errwarn.h" 
#include "intnum.h" 
#include "expr.h" 
 
#include "bytecode.h" 
 
 
typedef struct bytecode_align { 
    /*@only@*/ yasm_expr *boundary;     /* alignment boundary */ 
 
    /* What to fill intervening locations with, NULL if using code_fill */ 
    /*@only@*/ /*@null@*/ yasm_expr *fill; 
 
    /* Maximum number of bytes to skip, NULL if no maximum. */ 
    /*@only@*/ /*@null@*/ yasm_expr *maxskip; 
 
    /* Code fill, NULL if using 0 fill */ 
    /*@null@*/ const unsigned char **code_fill; 
} bytecode_align; 
 
static void bc_align_destroy(void *contents); 
static void bc_align_print(const void *contents, FILE *f, int indent_level); 
static void bc_align_finalize(yasm_bytecode *bc, yasm_bytecode *prev_bc); 
static int bc_align_calc_len(yasm_bytecode *bc, yasm_bc_add_span_func add_span, 
                             void *add_span_data); 
static int bc_align_expand(yasm_bytecode *bc, int span, long old_val, 
                           long new_val, /*@out@*/ long *neg_thres, 
                           /*@out@*/ long *pos_thres); 
static int bc_align_tobytes(yasm_bytecode *bc, unsigned char **bufp, 
                            unsigned char *bufstart, void *d, 
                            yasm_output_value_func output_value, 
                            /*@null@*/ yasm_output_reloc_func output_reloc); 
 
static const yasm_bytecode_callback bc_align_callback = { 
    bc_align_destroy, 
    bc_align_print, 
    bc_align_finalize, 
    NULL, 
    bc_align_calc_len, 
    bc_align_expand, 
    bc_align_tobytes, 
    YASM_BC_SPECIAL_OFFSET 
}; 
 
 
static void 
bc_align_destroy(void *contents) 
{ 
    bytecode_align *align = (bytecode_align *)contents; 
    if (align->boundary) 
        yasm_expr_destroy(align->boundary); 
    if (align->fill) 
        yasm_expr_destroy(align->fill); 
    if (align->maxskip) 
        yasm_expr_destroy(align->maxskip); 
    yasm_xfree(contents); 
} 
 
static void 
bc_align_print(const void *contents, FILE *f, int indent_level) 
{ 
    const bytecode_align *align = (const bytecode_align *)contents; 
    fprintf(f, "%*s_Align_\n", indent_level, ""); 
    fprintf(f, "%*sBoundary=", indent_level, ""); 
    yasm_expr_print(align->boundary, f); 
    fprintf(f, "\n%*sFill=", indent_level, ""); 
    yasm_expr_print(align->fill, f); 
    fprintf(f, "\n%*sMax Skip=", indent_level, ""); 
    yasm_expr_print(align->maxskip, f); 
    fprintf(f, "\n"); 
} 
 
static void 
bc_align_finalize(yasm_bytecode *bc, yasm_bytecode *prev_bc) 
{ 
    bytecode_align *align = (bytecode_align *)bc->contents; 
    if (!yasm_expr_get_intnum(&align->boundary, 0)) 
        yasm_error_set(YASM_ERROR_NOT_CONSTANT, 
                       N_("align boundary must be a constant")); 
    if (align->fill && !yasm_expr_get_intnum(&align->fill, 0)) 
        yasm_error_set(YASM_ERROR_NOT_CONSTANT, 
                       N_("align fill must be a constant")); 
    if (align->maxskip && !yasm_expr_get_intnum(&align->maxskip, 0)) 
        yasm_error_set(YASM_ERROR_NOT_CONSTANT, 
                       N_("align maximum skip must be a constant")); 
} 
 
static int 
bc_align_calc_len(yasm_bytecode *bc, yasm_bc_add_span_func add_span, 
                  void *add_span_data) 
{ 
    long neg_thres = 0; 
    long pos_thres = 0; 
 
    if (bc_align_expand(bc, 0, 0, (long)bc->offset, &neg_thres, 
                        &pos_thres) < 0) 
        return -1; 
 
    return 0; 
} 
 
static int 
bc_align_expand(yasm_bytecode *bc, int span, long old_val, long new_val, 
                /*@out@*/ long *neg_thres, /*@out@*/ long *pos_thres) 
{ 
    bytecode_align *align = (bytecode_align *)bc->contents; 
    unsigned long end; 
    unsigned long boundary = 
        yasm_intnum_get_uint(yasm_expr_get_intnum(&align->boundary, 0)); 
 
    if (boundary == 0) { 
        bc->len = 0; 
        *pos_thres = new_val; 
        return 0; 
    } 
 
    end = (unsigned long)new_val; 
    if ((unsigned long)new_val & (boundary-1)) 
        end = ((unsigned long)new_val & ~(boundary-1)) + boundary; 
 
    *pos_thres = (long)end; 
    bc->len = end - (unsigned long)new_val; 
 
    if (align->maxskip) { 
        unsigned long maxskip = 
            yasm_intnum_get_uint(yasm_expr_get_intnum(&align->maxskip, 0)); 
        if (bc->len > maxskip) { 
            *pos_thres = (long)end-maxskip-1; 
            bc->len = 0; 
        } 
    } 
    return 1; 
} 
 
static int 
bc_align_tobytes(yasm_bytecode *bc, unsigned char **bufp, 
                 unsigned char *bufstart, void *d, 
                 yasm_output_value_func output_value, 
                 /*@unused@*/ yasm_output_reloc_func output_reloc) 
{ 
    bytecode_align *align = (bytecode_align *)bc->contents; 
    unsigned long len; 
    unsigned long boundary = 
        yasm_intnum_get_uint(yasm_expr_get_intnum(&align->boundary, 0)); 
 
    if (boundary == 0) 
        return 0; 
    else { 
        unsigned long end = bc->offset; 
        if (bc->offset & (boundary-1)) 
            end = (bc->offset & ~(boundary-1)) + boundary; 
        len = end - bc->offset; 
        if (len == 0) 
            return 0; 
        if (align->maxskip) { 
            unsigned long maxskip = 
                yasm_intnum_get_uint(yasm_expr_get_intnum(&align->maxskip, 0)); 
            if (len > maxskip) 
                return 0; 
        } 
    } 
 
    if (align->fill) { 
        unsigned long v; 
        v = yasm_intnum_get_uint(yasm_expr_get_intnum(&align->fill, 0)); 
        memset(*bufp, (int)v, len); 
        *bufp += len; 
    } else if (align->code_fill) { 
        unsigned long maxlen = 15; 
        while (!align->code_fill[maxlen] && maxlen>0) 
            maxlen--; 
        if (maxlen == 0) { 
            yasm_error_set(YASM_ERROR_GENERAL, 
                           N_("could not find any code alignment size")); 
            return 1; 
        } 
 
        /* Fill with maximum code fill as much as possible */ 
        while (len > maxlen) { 
            memcpy(*bufp, align->code_fill[maxlen], maxlen); 
            *bufp += maxlen; 
            len -= maxlen; 
        } 
 
        if (!align->code_fill[len]) { 
            yasm_error_set(YASM_ERROR_VALUE, 
                           N_("invalid alignment size %d"), len); 
            return 1; 
        } 
        /* Handle rest of code fill */ 
        memcpy(*bufp, align->code_fill[len], len); 
        *bufp += len; 
    } else { 
        /* Just fill with 0 */ 
        memset(*bufp, 0, len); 
        *bufp += len; 
    } 
    return 0; 
} 
 
yasm_bytecode * 
yasm_bc_create_align(yasm_expr *boundary, yasm_expr *fill, 
                     yasm_expr *maxskip, const unsigned char **code_fill, 
                     unsigned long line) 
{ 
    bytecode_align *align = yasm_xmalloc(sizeof(bytecode_align)); 
 
    align->boundary = boundary; 
    align->fill = fill; 
    align->maxskip = maxskip; 
    align->code_fill = code_fill; 
 
    return yasm_bc_create_common(&bc_align_callback, align, line); 
}