4 * Copyright (C) 2005-2007 Peter Johnson
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND OTHER CONTRIBUTORS ``AS IS''
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR OTHER CONTRIBUTORS BE
19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25 * POSSIBILITY OF SUCH DAMAGE.
29 #include "libyasm-stdint.h"
41 typedef struct bytecode_org {
42 unsigned long start; /* target starting offset within section */
43 unsigned long fill; /* fill value */
46 static void bc_org_destroy(void *contents);
47 static void bc_org_print(const void *contents, FILE *f, int indent_level);
48 static void bc_org_finalize(yasm_bytecode *bc, yasm_bytecode *prev_bc);
49 static int bc_org_calc_len(yasm_bytecode *bc, yasm_bc_add_span_func add_span,
51 static int bc_org_expand(yasm_bytecode *bc, int span, long old_val,
52 long new_val, /*@out@*/ long *neg_thres,
53 /*@out@*/ long *pos_thres);
54 static int bc_org_tobytes(yasm_bytecode *bc, unsigned char **bufp,
55 unsigned char *bufstart, void *d,
56 yasm_output_value_func output_value,
57 /*@null@*/ yasm_output_reloc_func output_reloc);
59 static const yasm_bytecode_callback bc_org_callback = {
67 YASM_BC_SPECIAL_OFFSET
72 bc_org_destroy(void *contents)
78 bc_org_print(const void *contents, FILE *f, int indent_level)
80 const bytecode_org *org = (const bytecode_org *)contents;
81 fprintf(f, "%*s_Org_\n", indent_level, "");
82 fprintf(f, "%*sStart=%lu\n", indent_level, "", org->start);
86 bc_org_finalize(yasm_bytecode *bc, yasm_bytecode *prev_bc)
91 bc_org_calc_len(yasm_bytecode *bc, yasm_bc_add_span_func add_span,
94 bytecode_org *org = (bytecode_org *)bc->contents;
96 long pos_thres = org->start;
98 if (bc_org_expand(bc, 0, 0, (long)bc->offset, &neg_thres, &pos_thres) < 0)
105 bc_org_expand(yasm_bytecode *bc, int span, long old_val, long new_val,
106 /*@out@*/ long *neg_thres, /*@out@*/ long *pos_thres)
108 bytecode_org *org = (bytecode_org *)bc->contents;
110 /* Check for overrun */
111 if ((unsigned long)new_val > org->start) {
112 yasm_error_set(YASM_ERROR_GENERAL,
113 N_("ORG overlap with already existing data"));
117 /* Generate space to start offset */
118 bc->len = org->start - new_val;
123 bc_org_tobytes(yasm_bytecode *bc, unsigned char **bufp,
124 unsigned char *bufstart, void *d,
125 yasm_output_value_func output_value,
126 /*@unused@*/ yasm_output_reloc_func output_reloc)
128 bytecode_org *org = (bytecode_org *)bc->contents;
129 unsigned long len, i;
131 /* Sanity check for overrun */
132 if (bc->offset > org->start) {
133 yasm_error_set(YASM_ERROR_GENERAL,
134 N_("ORG overlap with already existing data"));
137 len = org->start - bc->offset;
138 for (i=0; i<len; i++)
139 YASM_WRITE_8(*bufp, org->fill); /* XXX: handle more than 8 bit? */
144 yasm_bc_create_org(unsigned long start, unsigned long fill, unsigned long line)
146 bytecode_org *org = yasm_xmalloc(sizeof(bytecode_org));
151 return yasm_bc_create_common(&bc_org_callback, org, line);