Merge tag 'nasm-2.15'
[nasm.git] / output / legacy.c
blob39793205e4120f810fba731ba5c28a896531f48b
1 /* ----------------------------------------------------------------------- *
3 * Copyright 2016-2017 The NASM Authors - All Rights Reserved
4 * See the file AUTHORS included with the NASM distribution for
5 * the specific copyright holders.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following
9 * conditions are met:
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 * ----------------------------------------------------------------------- */
35 * output/legacy.c
37 * Mangle a struct out_data to match the rather bizarre legacy
38 * backend interface.
40 * The "data" parameter for the output function points to a "int64_t",
41 * containing the address of the target in question, unless the type is
42 * OUT_RAWDATA, in which case it points to an "uint8_t"
43 * array.
45 * Exceptions are OUT_RELxADR, which denote an x-byte relocation
46 * which will be a relative jump. For this we need to know the
47 * distance in bytes from the start of the relocated record until
48 * the end of the containing instruction. _This_ is what is stored
49 * in the size part of the parameter, in this case.
51 * Also OUT_RESERVE denotes reservation of N bytes of BSS space,
52 * and the contents of the "data" parameter is irrelevant.
55 #include "nasm.h"
56 #include "outlib.h"
58 void nasm_do_legacy_output(const struct out_data *data)
60 const void *dptr = data->data;
61 enum out_type type = data->type;
62 int32_t tsegment = data->tsegment;
63 int32_t twrt = data->twrt;
64 uint64_t size = data->size;
66 switch (data->type) {
67 case OUT_RELADDR:
68 switch (data->size) {
69 case 1:
70 type = OUT_REL1ADR;
71 break;
72 case 2:
73 type = OUT_REL2ADR;
74 break;
75 case 4:
76 type = OUT_REL4ADR;
77 break;
78 case 8:
79 type = OUT_REL8ADR;
80 break;
81 default:
82 panic();
83 break;
86 dptr = &data->toffset;
87 size = data->relbase - data->offset;
88 break;
90 case OUT_SEGMENT:
91 type = OUT_ADDRESS;
92 dptr = zero_buffer;
93 size = (data->sign == OUT_SIGNED) ? -data->size : data->size;
94 tsegment |= 1;
95 break;
97 case OUT_ADDRESS:
98 dptr = &data->toffset;
99 size = (data->sign == OUT_SIGNED) ? -data->size : data->size;
100 break;
102 case OUT_RAWDATA:
103 case OUT_RESERVE:
104 tsegment = twrt = NO_SEG;
105 break;
107 case OUT_ZERODATA:
108 tsegment = twrt = NO_SEG;
109 type = OUT_RAWDATA;
110 dptr = zero_buffer;
111 while (size > ZERO_BUF_SIZE) {
112 ofmt->legacy_output(data->segment, dptr, type,
113 ZERO_BUF_SIZE, tsegment, twrt);
114 size -= ZERO_BUF_SIZE;
116 break;
118 default:
119 panic();
120 break;
123 ofmt->legacy_output(data->segment, dptr, type, size, tsegment, twrt);