|
| 1 | +/** |
| 2 | + * Copyright (c) 2021 Brian Callahan <bcallah@openbsd.org> |
| 3 | + * |
| 4 | + * Permission to use, copy, modify, and distribute this software for any |
| 5 | + * purpose with or without fee is hereby granted, provided that the above |
| 6 | + * copyright notice and this permission notice appear in all copies. |
| 7 | + * |
| 8 | + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
| 9 | + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 10 | + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
| 11 | + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 12 | + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 13 | + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
| 14 | + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 15 | + */ |
| 16 | + |
| 17 | +import std.stdio; |
| 18 | +import std.file; |
| 19 | +import std.conv; |
| 20 | +import std.string; |
| 21 | +import std.algorithm; |
| 22 | + |
| 23 | +/** |
| 24 | + * All object files together in one array. |
| 25 | + */ |
| 26 | +private ubyte[] rawobjs; |
| 27 | + |
| 28 | +/** |
| 29 | + * Final binary. |
| 30 | + */ |
| 31 | +private ubyte[] binary; |
| 32 | + |
| 33 | +/** |
| 34 | + * Address counter. |
| 35 | + * Starts at 100h, because CP/M. |
| 36 | + */ |
| 37 | +private size_t addr = 0x100; |
| 38 | + |
| 39 | +/** |
| 40 | + * Struct holding name and matching calculated addresses. |
| 41 | + * collect.addr is a size_t to detect address overflow. |
| 42 | + */ |
| 43 | +struct collect |
| 44 | +{ |
| 45 | + string name; /// Symbol name |
| 46 | + size_t addr; /// Symbol address |
| 47 | +}; |
| 48 | + |
| 49 | +/** |
| 50 | + * Collection of addresses. |
| 51 | + */ |
| 52 | +private collect[] collection; |
| 53 | + |
| 54 | +/** |
| 55 | + * Error messages. |
| 56 | + */ |
| 57 | +private int error(string msg) |
| 58 | +{ |
| 59 | + stderr.writeln("l80: error: " ~ msg); |
| 60 | + return 1; |
| 61 | +} |
| 62 | + |
| 63 | +/** |
| 64 | + * Check if symbol exists in the collection. |
| 65 | + */ |
| 66 | +private bool dupname(string name) |
| 67 | +{ |
| 68 | + for (size_t i = 0; i < collection.length; i++) { |
| 69 | + if (name == collection[i].name) |
| 70 | + return true; |
| 71 | + } |
| 72 | + |
| 73 | + return false; |
| 74 | +} |
| 75 | + |
| 76 | +/** |
| 77 | + * Pass 1: Collect symbol names. |
| 78 | + */ |
| 79 | +private int collect1() |
| 80 | +{ |
| 81 | + for (size_t i = 0; i < rawobjs.length; i++) { |
| 82 | + if (rawobjs[i] == '\0') { |
| 83 | + /* Skip control byte. */ |
| 84 | + ++i; |
| 85 | + |
| 86 | + /* Increment address counter. */ |
| 87 | + ++addr; |
| 88 | + |
| 89 | + /* Binary has a maximum size of 65,280 bytes (FF00h). */ |
| 90 | + if (addr > 65280) |
| 91 | + return error("final binary exceeds 65,280 bytes"); |
| 92 | + } else if (rawobjs[i] == '\001') { |
| 93 | + string name; |
| 94 | + |
| 95 | + /* Skip control byte. */ |
| 96 | + ++i; |
| 97 | + |
| 98 | + /* Get symbol name, put in collect struct. */ |
| 99 | + while (rawobjs[i] != '\001') { |
| 100 | + name ~= rawobjs[i++]; |
| 101 | + if (i == rawobjs.length) |
| 102 | + return error("unterminated symbol"); |
| 103 | + } |
| 104 | + |
| 105 | + /* Make sure there is actually a symbol here. */ |
| 106 | + if (name.empty) |
| 107 | + return error("symbol marker with no symbol inside"); |
| 108 | + |
| 109 | + /* Make sure name isn't already in the collection. */ |
| 110 | + if (dupname(name) == true) |
| 111 | + return error("duplicate symbol: " ~ name); |
| 112 | + |
| 113 | + /* Add to collection table. */ |
| 114 | + collect newcollect = { name, addr }; |
| 115 | + collection ~= newcollect; |
| 116 | + } else if (rawobjs[i] == '\002') { |
| 117 | + /* Skip control byte. */ |
| 118 | + ++i; |
| 119 | + |
| 120 | + /* Ignore the references during pass 1. */ |
| 121 | + while (rawobjs[i] != '\002') { |
| 122 | + ++i; |
| 123 | + if (i == rawobjs.length) |
| 124 | + return error("unterminated symbol reference"); |
| 125 | + } |
| 126 | + |
| 127 | + /* Increment address counter. */ |
| 128 | + addr += 2; |
| 129 | + |
| 130 | + /* Binary has a maximum size of 65,280 bytes (FF00h). */ |
| 131 | + if (addr > 65280) |
| 132 | + return error("final binary exceeds 65,280 bytes"); |
| 133 | + } else { |
| 134 | + /* This should never happen. */ |
| 135 | + return error("unknown control byte: " ~ to!string(rawobjs[i])); |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + return 0; |
| 140 | +} |
| 141 | + |
| 142 | +/** |
| 143 | + * Pass 2: Write out final binary. |
| 144 | + */ |
| 145 | +private int process2() |
| 146 | +{ |
| 147 | + for (size_t i = 0; i < rawobjs.length; i++) { |
| 148 | + if (rawobjs[i] == '\0') { |
| 149 | + /* Skip control byte. */ |
| 150 | + ++i; |
| 151 | + |
| 152 | + /* Write byte to final binary. */ |
| 153 | + binary ~= rawobjs[i]; |
| 154 | + } else if (rawobjs[i] == '\001') { |
| 155 | + /* Skip control byte. */ |
| 156 | + ++i; |
| 157 | + |
| 158 | + /* Ignore the declarations during pass 2. */ |
| 159 | + while (rawobjs[i] != '\001') { |
| 160 | + ++i; |
| 161 | + if (i == rawobjs.length) |
| 162 | + return error("unterminated symbol"); |
| 163 | + } |
| 164 | + } else if (rawobjs[i] == '\002') { |
| 165 | + bool found = false; |
| 166 | + string name; |
| 167 | + |
| 168 | + /* Skip control byte. */ |
| 169 | + ++i; |
| 170 | + |
| 171 | + while (rawobjs[i] != '\002') { |
| 172 | + name ~= rawobjs[i]; |
| 173 | + ++i; |
| 174 | + |
| 175 | + if (i == rawobjs.length) |
| 176 | + return error("unterminated symbol reference"); |
| 177 | + } |
| 178 | + |
| 179 | + /* Make sure there is actually a symbol here. */ |
| 180 | + if (name.empty) |
| 181 | + return error("symbol marker with no symbol inside"); |
| 182 | + |
| 183 | + /* Output symbol. */ |
| 184 | + for (size_t j = 0; j < collection.length; j++) { |
| 185 | + if (name == collection[j].name) { |
| 186 | + binary ~= cast(ubyte)(collection[j].addr & 0xff); |
| 187 | + binary ~= cast(ubyte)((collection[j].addr >> 8) & 0xff); |
| 188 | + found = true; |
| 189 | + break; |
| 190 | + } |
| 191 | + } |
| 192 | + |
| 193 | + /* If symbol was not found, error out. */ |
| 194 | + if (found == false) |
| 195 | + return error("undefined reference: " ~ name); |
| 196 | + } else { |
| 197 | + /* This should never happen. */ |
| 198 | + return error("unknown control byte: " ~ to!string(rawobjs[i])); |
| 199 | + } |
| 200 | + } |
| 201 | + |
| 202 | + return 0; |
| 203 | +} |
| 204 | + |
| 205 | +/** |
| 206 | + * After all code is emitted, write it out to a file. |
| 207 | + */ |
| 208 | +private void fileWrite(string outfile) |
| 209 | +{ |
| 210 | + import std.file : write; |
| 211 | + |
| 212 | + write(outfile, binary); |
| 213 | +} |
| 214 | + |
| 215 | +int main(string[] args) |
| 216 | +{ |
| 217 | + int ret; |
| 218 | + |
| 219 | + if (args.length < 3) { |
| 220 | + stderr.writeln("usage: l80 file.com file1.obj [file2.obj ...]"); |
| 221 | + return 1; |
| 222 | + } |
| 223 | + |
| 224 | + /** |
| 225 | + * Write all object files into a single buffer. |
| 226 | + */ |
| 227 | + foreach (size_t i; 2 .. args.length) { |
| 228 | + auto objsplit = args[i].findSplit(".obj"); |
| 229 | + |
| 230 | + /* Objects must end in .obj or .lib. */ |
| 231 | + if (objsplit[1].empty) { |
| 232 | + auto libsplit = args[i].findSplit(".lib"); |
| 233 | + if (libsplit[1].empty) |
| 234 | + return error("object files must end in \".obj\" or \".lib\""); |
| 235 | + } |
| 236 | + |
| 237 | + rawobjs ~= cast(ubyte[])read(args[i]); |
| 238 | + } |
| 239 | + |
| 240 | + ret = collect1(); |
| 241 | + if (ret == 0) { |
| 242 | + ret = process2(); |
| 243 | + if (ret == 0) |
| 244 | + fileWrite(args[1]); |
| 245 | + } |
| 246 | + |
| 247 | + return ret; |
| 248 | +} |
0 commit comments