Properly keep track of the base of relative relocations
[nasm.git] / output / legacy.c
blob3fc95ee81d26d31473d44ea68e9836b0a0509613
1 /* ----------------------------------------------------------------------- *
3 * Copyright 2016 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 /* fall through */
94 case OUT_ADDRESS:
95 dptr = &data->toffset;
96 size = (data->sign == OUT_SIGNED) ? -data->size : data->size;
97 break;
99 case OUT_RAWDATA:
100 case OUT_RESERVE:
101 tsegment = twrt = NO_SEG;
102 break;
104 default:
105 panic();
106 break;
109 ofmt->legacy_output(data->segment, dptr, type, size, tsegment, twrt);