| 1 |
/* SPDX-License-Identifier: BSD-2-Clause */ |
| 2 |
/* Copyright 1996-2022 The NASM Authors - All Rights Reserved */ |
| 3 |
|
| 4 |
/* |
| 5 |
* outieee.c output routines for the Netwide Assembler to produce |
| 6 |
* IEEE-std object files |
| 7 |
*/ |
| 8 |
|
| 9 |
/* notes: I have tried to make this correspond to the IEEE version |
| 10 |
* of the standard, specifically the primary ASCII version. It should |
| 11 |
* be trivial to create the binary version given this source (which is |
| 12 |
* one of MANY things that have to be done to make this correspond to |
| 13 |
* the hp-microtek version of the standard). |
| 14 |
* |
| 15 |
* 16-bit support is assumed to use 24-bit addresses |
| 16 |
* The linker can sort out segmentation-specific stuff |
| 17 |
* if it keeps track of externals |
| 18 |
* in terms of being relative to section bases |
| 19 |
* |
| 20 |
* A non-standard variable type, the 'Yn' variable, has been introduced. |
| 21 |
* Basically it is a reference to extern 'n'- denoting the low limit |
| 22 |
* (L-variable) of the section that extern 'n' is defined in. Like the |
| 23 |
* x variable, there may be no explicit assignment to it, it is derived |
| 24 |
* from the public definition corresponding to the extern name. This |
| 25 |
* is required because the one thing the mufom guys forgot to do well was |
| 26 |
* take into account segmented architectures. |
| 27 |
* |
| 28 |
* I use comment classes for various things and these are undefined by |
| 29 |
* the standard. |
| 30 |
* |
| 31 |
* Debug info should be considered totally non-standard (local labels are |
| 32 |
* standard but linenum records are not covered by the standard. |
| 33 |
* Type defs have the standard format but absolute meanings for ordinal |
| 34 |
* types are not covered by the standard.) |
| 35 |
* |
| 36 |
* David Lindauer, LADsoft |
| 37 |
*/ |
| 38 |
#include "compiler.h" |
| 39 |
|
| 40 |
#include <time.h> |
| 41 |
#include <ctype.h> /* For toupper() */ |
| 42 |
#include "nctype.h" |
| 43 |
|
| 44 |
#include "nasm.h" |
| 45 |
#include "nasmlib.h" |
| 46 |
#include "asmutil.h" |
| 47 |
#include "error.h" |
| 48 |
#include "ver.h" |
| 49 |
|
| 50 |
#include "outform.h" |
| 51 |
#include "outlib.h" |
| 52 |
|
| 53 |
#ifdef OF_IEEE |
| 54 |
|
| 55 |
#define ARRAY_BOT 0x1 |
| 56 |
|
| 57 |
static char ieee_infile[FILENAME_MAX]; |
| 58 |
static bool ieee_uppercase; |
| 59 |
|
| 60 |
static bool any_segs; |
| 61 |
static int arrindex; |
| 62 |
|
| 63 |
#define HUNKSIZE 1024 /* Size of the data hunk */ |
| 64 |
#define EXT_BLKSIZ 512 |
| 65 |
#define LDPERLINE 32 /* bytes per line in output */ |
| 66 |
|
| 67 |
struct ieeeSection; |
| 68 |
|
| 69 |
struct LineNumber { |
| 70 |
struct LineNumber *next; |
| 71 |
struct ieeeSection *segment; |
| 72 |
int32_t offset; |
| 73 |
int32_t lineno; |
| 74 |
}; |
| 75 |
|
| 76 |
static struct FileName { |
| 77 |
struct FileName *next; |
| 78 |
char *name; |
| 79 |
int32_t index; |
| 80 |
} *fnhead, **fntail; |
| 81 |
|
| 82 |
static struct Array { |
| 83 |
struct Array *next; |
| 84 |
unsigned size; |
| 85 |
int basetype; |
| 86 |
} *arrhead, **arrtail; |
| 87 |
|
| 88 |
static struct ieeePublic { |
| 89 |
struct ieeePublic *next; |
| 90 |
char *name; |
| 91 |
int32_t offset; |
| 92 |
int32_t segment; /* only if it's far-absolute */ |
| 93 |
int32_t index; |
| 94 |
int type; /* for debug purposes */ |
| 95 |
} *fpubhead, **fpubtail, *last_defined; |
| 96 |
|
| 97 |
static struct ieeeExternal { |
| 98 |
struct ieeeExternal *next; |
| 99 |
char *name; |
| 100 |
int32_t commonsize; |
| 101 |
} *exthead, **exttail; |
| 102 |
|
| 103 |
static int externals; |
| 104 |
|
| 105 |
static struct ExtBack { |
| 106 |
struct ExtBack *next; |
| 107 |
int index[EXT_BLKSIZ]; |
| 108 |
} *ebhead, **ebtail; |
| 109 |
|
| 110 |
/* NOTE: the first segment MUST be the lineno segment */ |
| 111 |
static struct ieeeSection { |
| 112 |
struct ieeeSection *next; |
| 113 |
char *name; |
| 114 |
struct ieeeObjData *data, *datacurr; |
| 115 |
struct ieeeFixupp *fptr, *flptr; |
| 116 |
int32_t index; /* the NASM segment id */ |
| 117 |
int32_t ieee_index; /* the IEEE-file segment index */ |
| 118 |
int32_t currentpos; |
| 119 |
int32_t align; /* can be SEG_ABS + absolute addr */ |
| 120 |
int32_t startpos; |
| 121 |
int32_t use32; /* is this segment 32-bit? */ |
| 122 |
int64_t pass_last_seen; |
| 123 |
struct ieeePublic *pubhead, **pubtail, *lochead, **loctail; |
| 124 |
enum { |
| 125 |
CMB_PRIVATE = 0, |
| 126 |
CMB_PUBLIC = 2, |
| 127 |
CMB_COMMON = 6 |
| 128 |
} combine; |
| 129 |
} *seghead, **segtail, *ieee_seg_needs_update; |
| 130 |
|
| 131 |
struct ieeeObjData { |
| 132 |
struct ieeeObjData *next; |
| 133 |
uint8_t data[HUNKSIZE]; |
| 134 |
}; |
| 135 |
|
| 136 |
struct ieeeFixupp { |
| 137 |
struct ieeeFixupp *next; |
| 138 |
enum { |
| 139 |
FT_SEG = 0, |
| 140 |
FT_REL = 1, |
| 141 |
FT_OFS = 2, |
| 142 |
FT_EXT = 3, |
| 143 |
FT_WRT = 4, |
| 144 |
FT_EXTREL = 5, |
| 145 |
FT_EXTWRT = 6, |
| 146 |
FT_EXTSEG = 7 |
| 147 |
} ftype; |
| 148 |
int16_t size; |
| 149 |
int32_t id1; |
| 150 |
int32_t id2; |
| 151 |
int32_t offset; |
| 152 |
int32_t addend; |
| 153 |
}; |
| 154 |
|
| 155 |
static int32_t ieee_entry_seg, ieee_entry_ofs; |
| 156 |
static int checksum; |
| 157 |
|
| 158 |
extern const struct ofmt of_ieee; |
| 159 |
static const struct dfmt ladsoft_debug_form; |
| 160 |
|
| 161 |
static void ieee_data_new(struct ieeeSection *); |
| 162 |
static void ieee_write_fixup(int32_t, int32_t, struct ieeeSection *, |
| 163 |
int, uint64_t, int32_t); |
| 164 |
static void ieee_install_fixup(struct ieeeSection *, struct ieeeFixupp *); |
| 165 |
static int32_t ieee_segment(char *, int *); |
| 166 |
static void ieee_write_file(void); |
| 167 |
static void ieee_write_byte(struct ieeeSection *, int); |
| 168 |
static void ieee_write_word(struct ieeeSection *, int); |
| 169 |
static void ieee_write_dword(struct ieeeSection *, int32_t); |
| 170 |
static void ieee_putascii(char *, ...); |
| 171 |
static void ieee_putcs(int); |
| 172 |
static int32_t ieee_putld(int32_t, int32_t, uint8_t *); |
| 173 |
static int32_t ieee_putlr(struct ieeeFixupp *); |
| 174 |
static void ieee_unqualified_name(char *, char *); |
| 175 |
|
| 176 |
/* |
| 177 |
* pup init |
| 178 |
*/ |
| 179 |
static void ieee_init(void) |
| 180 |
{ |
| 181 |
strlcpy(ieee_infile, inname, sizeof(ieee_infile)); |
| 182 |
any_segs = false; |
| 183 |
fpubhead = NULL; |
| 184 |
fpubtail = &fpubhead; |
| 185 |
exthead = NULL; |
| 186 |
exttail = &exthead; |
| 187 |
externals = 1; |
| 188 |
ebhead = NULL; |
| 189 |
ebtail = &ebhead; |
| 190 |
seghead = ieee_seg_needs_update = NULL; |
| 191 |
segtail = &seghead; |
| 192 |
ieee_entry_seg = NO_SEG; |
| 193 |
ieee_uppercase = false; |
| 194 |
checksum = 0; |
| 195 |
} |
| 196 |
|
| 197 |
/* |
| 198 |
* Rundown |
| 199 |
*/ |
| 200 |
static void ieee_cleanup(void) |
| 201 |
{ |
| 202 |
ieee_write_file(); |
| 203 |
dfmt->cleanup(); |
| 204 |
while (seghead) { |
| 205 |
struct ieeeSection *segtmp = seghead; |
| 206 |
seghead = seghead->next; |
| 207 |
while (segtmp->pubhead) { |
| 208 |
struct ieeePublic *pubtmp = segtmp->pubhead; |
| 209 |
segtmp->pubhead = pubtmp->next; |
| 210 |
nasm_free(pubtmp); |
| 211 |
} |
| 212 |
while (segtmp->fptr) { |
| 213 |
struct ieeeFixupp *fixtmp = segtmp->fptr; |
| 214 |
segtmp->fptr = fixtmp->next; |
| 215 |
nasm_free(fixtmp); |
| 216 |
} |
| 217 |
while (segtmp->data) { |
| 218 |
struct ieeeObjData *dattmp = segtmp->data; |
| 219 |
segtmp->data = dattmp->next; |
| 220 |
nasm_free(dattmp); |
| 221 |
} |
| 222 |
nasm_free(segtmp); |
| 223 |
} |
| 224 |
while (fpubhead) { |
| 225 |
struct ieeePublic *pubtmp = fpubhead; |
| 226 |
fpubhead = fpubhead->next; |
| 227 |
nasm_free(pubtmp); |
| 228 |
} |
| 229 |
while (exthead) { |
| 230 |
struct ieeeExternal *exttmp = exthead; |
| 231 |
exthead = exthead->next; |
| 232 |
nasm_free(exttmp); |
| 233 |
} |
| 234 |
while (ebhead) { |
| 235 |
struct ExtBack *ebtmp = ebhead; |
| 236 |
ebhead = ebhead->next; |
| 237 |
nasm_free(ebtmp); |
| 238 |
} |
| 239 |
} |
| 240 |
|
| 241 |
/* |
| 242 |
* callback for labels |
| 243 |
*/ |
| 244 |
static void ieee_deflabel(char *name, int32_t segment, |
| 245 |
int64_t offset, int is_global, char *special) |
| 246 |
{ |
| 247 |
/* |
| 248 |
* We have three cases: |
| 249 |
* |
| 250 |
* (i) `segment' is a segment-base. If so, set the name field |
| 251 |
* for the segment structure it refers to, and then |
| 252 |
* return. |
| 253 |
* |
| 254 |
* (ii) `segment' is one of our segments, or a SEG_ABS segment. |
| 255 |
* Save the label position for later output of a PUBDEF record. |
| 256 |
* |
| 257 |
* |
| 258 |
* (iii) `segment' is not one of our segments. Save the label |
| 259 |
* position for later output of an EXTDEF. |
| 260 |
*/ |
| 261 |
struct ieeeExternal *ext; |
| 262 |
struct ExtBack *eb; |
| 263 |
struct ieeeSection *seg; |
| 264 |
int i; |
| 265 |
|
| 266 |
if (special) |
| 267 |
nasm_nonfatal("unrecognised symbol type `%s'", special); |
| 268 |
/* |
| 269 |
* First check for the double-period, signifying something |
| 270 |
* unusual. |
| 271 |
*/ |
| 272 |
if (name[0] == '.' && name[1] == '.' && name[2] != '@') { |
| 273 |
if (!strcmp(name, "..start")) { |
| 274 |
ieee_entry_seg = segment; |
| 275 |
ieee_entry_ofs = offset; |
| 276 |
} |
| 277 |
return; |
| 278 |
} |
| 279 |
|
| 280 |
/* |
| 281 |
* Case (i): |
| 282 |
*/ |
| 283 |
if (ieee_seg_needs_update) { |
| 284 |
ieee_seg_needs_update->name = name; |
| 285 |
return; |
| 286 |
} |
| 287 |
if (segment < SEG_ABS && segment != NO_SEG && segment % 2) |
| 288 |
return; |
| 289 |
|
| 290 |
/* |
| 291 |
* case (ii) |
| 292 |
*/ |
| 293 |
if (segment >= SEG_ABS) { |
| 294 |
/* |
| 295 |
* SEG_ABS subcase of (ii). |
| 296 |
*/ |
| 297 |
if (is_global) { |
| 298 |
struct ieeePublic *pub; |
| 299 |
|
| 300 |
pub = *fpubtail = nasm_malloc(sizeof(*pub)); |
| 301 |
fpubtail = &pub->next; |
| 302 |
pub->next = NULL; |
| 303 |
pub->name = name; |
| 304 |
pub->offset = offset; |
| 305 |
pub->segment = segment & ~SEG_ABS; |
| 306 |
} |
| 307 |
return; |
| 308 |
} |
| 309 |
|
| 310 |
for (seg = seghead; seg && is_global; seg = seg->next) |
| 311 |
if (seg->index == segment) { |
| 312 |
struct ieeePublic *pub; |
| 313 |
|
| 314 |
last_defined = pub = *seg->pubtail = nasm_malloc(sizeof(*pub)); |
| 315 |
seg->pubtail = &pub->next; |
| 316 |
pub->next = NULL; |
| 317 |
pub->name = name; |
| 318 |
pub->offset = offset; |
| 319 |
pub->index = seg->ieee_index; |
| 320 |
pub->segment = -1; |
| 321 |
return; |
| 322 |
} |
| 323 |
|
| 324 |
/* |
| 325 |
* Case (iii). |
| 326 |
*/ |
| 327 |
if (is_global) { |
| 328 |
ext = *exttail = nasm_malloc(sizeof(*ext)); |
| 329 |
ext->next = NULL; |
| 330 |
exttail = &ext->next; |
| 331 |
ext->name = name; |
| 332 |
if (is_global == 2) |
| 333 |
ext->commonsize = offset; |
| 334 |
else |
| 335 |
ext->commonsize = 0; |
| 336 |
i = segment / 2; |
| 337 |
eb = ebhead; |
| 338 |
if (!eb) { |
| 339 |
eb = *ebtail = nasm_zalloc(sizeof(*eb)); |
| 340 |
eb->next = NULL; |
| 341 |
ebtail = &eb->next; |
| 342 |
} |
| 343 |
while (i > EXT_BLKSIZ) { |
| 344 |
if (eb && eb->next) |
| 345 |
eb = eb->next; |
| 346 |
else { |
| 347 |
eb = *ebtail = nasm_zalloc(sizeof(*eb)); |
| 348 |
eb->next = NULL; |
| 349 |
ebtail = &eb->next; |
| 350 |
} |
| 351 |
i -= EXT_BLKSIZ; |
| 352 |
} |
| 353 |
eb->index[i] = externals++; |
| 354 |
} |
| 355 |
|
| 356 |
} |
| 357 |
|
| 358 |
/* |
| 359 |
* Put data out |
| 360 |
*/ |
| 361 |
static void ieee_out(const struct out_data *out) |
| 362 |
{ |
| 363 |
OUT_LEGACY(out,segto,data,type,size,segment,wrt); |
| 364 |
const uint8_t *ucdata; |
| 365 |
int32_t ldata; |
| 366 |
struct ieeeSection *seg; |
| 367 |
|
| 368 |
/* |
| 369 |
* If `any_segs' is still false, we must define a default |
| 370 |
* segment. |
| 371 |
*/ |
| 372 |
if (!any_segs) { |
| 373 |
int tempint; /* ignored */ |
| 374 |
if (segto != ieee_segment("__NASMDEFSEG", &tempint)) |
| 375 |
nasm_panic("strange segment conditions in IEEE driver"); |
| 376 |
} |
| 377 |
|
| 378 |
/* |
| 379 |
* Find the segment we are targeting. |
| 380 |
*/ |
| 381 |
for (seg = seghead; seg; seg = seg->next) |
| 382 |
if (seg->index == segto) |
| 383 |
break; |
| 384 |
if (!seg) |
| 385 |
nasm_panic("code directed to nonexistent segment?"); |
| 386 |
|
| 387 |
if (type == OUT_RAWDATA) { |
| 388 |
ucdata = data; |
| 389 |
while (size--) |
| 390 |
ieee_write_byte(seg, *ucdata++); |
| 391 |
} else if (type == OUT_ADDRESS || type == OUT_REL2ADR || |
| 392 |
type == OUT_REL4ADR) { |
| 393 |
if (type == OUT_ADDRESS) |
| 394 |
size = abs((int)size); |
| 395 |
else if (segment == NO_SEG) |
| 396 |
nasm_nonfatal("relative call to absolute address not" |
| 397 |
" supported by IEEE format"); |
| 398 |
ldata = *(int64_t *)data; |
| 399 |
if (type == OUT_REL2ADR) |
| 400 |
ldata += (size - 2); |
| 401 |
if (type == OUT_REL4ADR) |
| 402 |
ldata += (size - 4); |
| 403 |
ieee_write_fixup(segment, wrt, seg, size, type, ldata); |
| 404 |
if (size == 2) |
| 405 |
ieee_write_word(seg, ldata); |
| 406 |
else |
| 407 |
ieee_write_dword(seg, ldata); |
| 408 |
} else if (type == OUT_RESERVE) { |
| 409 |
while (size--) |
| 410 |
ieee_write_byte(seg, 0); |
| 411 |
} |
| 412 |
} |
| 413 |
|
| 414 |
static void ieee_data_new(struct ieeeSection *segto) |
| 415 |
{ |
| 416 |
|
| 417 |
if (!segto->data) |
| 418 |
segto->data = segto->datacurr = |
| 419 |
nasm_malloc(sizeof(*(segto->datacurr))); |
| 420 |
else |
| 421 |
segto->datacurr = segto->datacurr->next = |
| 422 |
nasm_malloc(sizeof(*(segto->datacurr))); |
| 423 |
segto->datacurr->next = NULL; |
| 424 |
} |
| 425 |
|
| 426 |
/* |
| 427 |
* this routine is unalduterated bloatware. I usually don't do this |
| 428 |
* but I might as well see what it is like on a harmless program. |
| 429 |
* If anyone wants to optimize this is a good canditate! |
| 430 |
*/ |
| 431 |
static void ieee_write_fixup(int32_t segment, int32_t wrt, |
| 432 |
struct ieeeSection *segto, int size, |
| 433 |
uint64_t realtype, int32_t offset) |
| 434 |
{ |
| 435 |
struct ieeeSection *target; |
| 436 |
struct ieeeFixupp s; |
| 437 |
|
| 438 |
/* Don't put a fixup for things NASM can calculate */ |
| 439 |
if (wrt == NO_SEG && segment == NO_SEG) |
| 440 |
return; |
| 441 |
|
| 442 |
s.ftype = -1; |
| 443 |
/* if it is a WRT offset */ |
| 444 |
if (wrt != NO_SEG) { |
| 445 |
s.ftype = FT_WRT; |
| 446 |
s.addend = offset; |
| 447 |
if (wrt >= SEG_ABS) |
| 448 |
s.id1 = -(wrt - SEG_ABS); |
| 449 |
else { |
| 450 |
if (wrt % 2 && realtype != OUT_REL2ADR |
| 451 |
&& realtype != OUT_REL4ADR) { |
| 452 |
wrt--; |
| 453 |
|
| 454 |
for (target = seghead; target; target = target->next) |
| 455 |
if (target->index == wrt) |
| 456 |
break; |
| 457 |
if (target) { |
| 458 |
s.id1 = target->ieee_index; |
| 459 |
for (target = seghead; target; target = target->next) |
| 460 |
if (target->index == segment) |
| 461 |
break; |
| 462 |
|
| 463 |
if (target) |
| 464 |
s.id2 = target->ieee_index; |
| 465 |
else { |
| 466 |
/* |
| 467 |
* Now we assume the segment field is being used |
| 468 |
* to hold an extern index |
| 469 |
*/ |
| 470 |
int32_t i = segment / 2; |
| 471 |
struct ExtBack *eb = ebhead; |
| 472 |
while (i > EXT_BLKSIZ) { |
| 473 |
if (eb) |
| 474 |
eb = eb->next; |
| 475 |
else |
| 476 |
break; |
| 477 |
i -= EXT_BLKSIZ; |
| 478 |
} |
| 479 |
/* if we have an extern decide the type and make a record |
| 480 |
*/ |
| 481 |
if (eb) { |
| 482 |
s.ftype = FT_EXTWRT; |
| 483 |
s.addend = 0; |
| 484 |
s.id2 = eb->index[i]; |
| 485 |
} else |
| 486 |
nasm_nonfatal("source of WRT must be an offset"); |
| 487 |
} |
| 488 |
|
| 489 |
} else |
| 490 |
nasm_panic("unrecognised WRT value in ieee_write_fixup"); |
| 491 |
} else |
| 492 |
nasm_nonfatal("target of WRT must be a section"); |
| 493 |
} |
| 494 |
s.size = size; |
| 495 |
ieee_install_fixup(segto, &s); |
| 496 |
return; |
| 497 |
} |
| 498 |
/* Pure segment fixup ? */ |
| 499 |
if (segment != NO_SEG) { |
| 500 |
s.ftype = FT_SEG; |
| 501 |
s.id1 = 0; |
| 502 |
if (segment >= SEG_ABS) { |
| 503 |
/* absolute far segment fixup */ |
| 504 |
s.id1 = -(segment - ~SEG_ABS); |
| 505 |
} else if (segment % 2) { |
| 506 |
/* fixup to named segment */ |
| 507 |
/* look it up */ |
| 508 |
for (target = seghead; target; target = target->next) |
| 509 |
if (target->index == segment - 1) |
| 510 |
break; |
| 511 |
if (target) |
| 512 |
s.id1 = target->ieee_index; |
| 513 |
else { |
| 514 |
/* |
| 515 |
* Now we assume the segment field is being used |
| 516 |
* to hold an extern index |
| 517 |
*/ |
| 518 |
int32_t i = segment / 2; |
| 519 |
struct ExtBack *eb = ebhead; |
| 520 |
while (i > EXT_BLKSIZ) { |
| 521 |
if (eb) |
| 522 |
eb = eb->next; |
| 523 |
else |
| 524 |
break; |
| 525 |
i -= EXT_BLKSIZ; |
| 526 |
} |
| 527 |
/* if we have an extern decide the type and make a record |
| 528 |
*/ |
| 529 |
if (eb) { |
| 530 |
if (realtype == OUT_REL2ADR || realtype == OUT_REL4ADR) { |
| 531 |
nasm_panic("Segment of a rel not supported in ieee_write_fixup"); |
| 532 |
} else { |
| 533 |
/* If we want the segment */ |
| 534 |
s.ftype = FT_EXTSEG; |
| 535 |
s.addend = 0; |
| 536 |
s.id1 = eb->index[i]; |
| 537 |
} |
| 538 |
|
| 539 |
} else |
| 540 |
/* If we get here the seg value doesn't make sense */ |
| 541 |
nasm_panic("unrecognised segment value in ieee_write_fixup"); |
| 542 |
} |
| 543 |
|
| 544 |
} else { |
| 545 |
/* Assume we are offsetting directly from a section |
| 546 |
* So look up the target segment |
| 547 |
*/ |
| 548 |
for (target = seghead; target; target = target->next) |
| 549 |
if (target->index == segment) |
| 550 |
break; |
| 551 |
if (target) { |
| 552 |
if (realtype == OUT_REL2ADR || realtype == OUT_REL4ADR) { |
| 553 |
/* PC rel to a known offset */ |
| 554 |
s.id1 = target->ieee_index; |
| 555 |
s.ftype = FT_REL; |
| 556 |
s.size = size; |
| 557 |
s.addend = offset; |
| 558 |
} else { |
| 559 |
/* We were offsetting from a seg */ |
| 560 |
s.id1 = target->ieee_index; |
| 561 |
s.ftype = FT_OFS; |
| 562 |
s.size = size; |
| 563 |
s.addend = offset; |
| 564 |
} |
| 565 |
} else { |
| 566 |
/* |
| 567 |
* Now we assume the segment field is being used |
| 568 |
* to hold an extern index |
| 569 |
*/ |
| 570 |
int32_t i = segment / 2; |
| 571 |
struct ExtBack *eb = ebhead; |
| 572 |
while (i > EXT_BLKSIZ) { |
| 573 |
if (eb) |
| 574 |
eb = eb->next; |
| 575 |
else |
| 576 |
break; |
| 577 |
i -= EXT_BLKSIZ; |
| 578 |
} |
| 579 |
/* if we have an extern decide the type and make a record |
| 580 |
*/ |
| 581 |
if (eb) { |
| 582 |
if (realtype == OUT_REL2ADR || realtype == OUT_REL4ADR) { |
| 583 |
s.ftype = FT_EXTREL; |
| 584 |
s.addend = 0; |
| 585 |
s.id1 = eb->index[i]; |
| 586 |
} else { |
| 587 |
/* else we want the external offset */ |
| 588 |
s.ftype = FT_EXT; |
| 589 |
s.addend = 0; |
| 590 |
s.id1 = eb->index[i]; |
| 591 |
} |
| 592 |
|
| 593 |
} else |
| 594 |
/* If we get here the seg value doesn't make sense */ |
| 595 |
nasm_panic("unrecognised segment value in ieee_write_fixup"); |
| 596 |
} |
| 597 |
} |
| 598 |
if (size != 2 && s.ftype == FT_SEG) |
| 599 |
nasm_nonfatal("IEEE format can only handle 2-byte" |
| 600 |
" segment base references"); |
| 601 |
s.size = size; |
| 602 |
ieee_install_fixup(segto, &s); |
| 603 |
return; |
| 604 |
} |
| 605 |
/* should never get here */ |
| 606 |
} |
| 607 |
static void ieee_install_fixup(struct ieeeSection *seg, |
| 608 |
struct ieeeFixupp *fix) |
| 609 |
{ |
| 610 |
struct ieeeFixupp *f; |
| 611 |
f = nasm_malloc(sizeof(struct ieeeFixupp)); |
| 612 |
memcpy(f, fix, sizeof(struct ieeeFixupp)); |
| 613 |
f->offset = seg->currentpos; |
| 614 |
seg->currentpos += fix->size; |
| 615 |
f->next = NULL; |
| 616 |
if (seg->fptr) |
| 617 |
seg->flptr = seg->flptr->next = f; |
| 618 |
else |
| 619 |
seg->fptr = seg->flptr = f; |
| 620 |
|
| 621 |
} |
| 622 |
|
| 623 |
/* |
| 624 |
* segment registry |
| 625 |
*/ |
| 626 |
static int32_t ieee_segment(char *name, int *bits) |
| 627 |
{ |
| 628 |
/* |
| 629 |
* We call the label manager here to define a name for the new |
| 630 |
* segment, and when our _own_ label-definition stub gets |
| 631 |
* called in return, it should register the new segment name |
| 632 |
* using the pointer it gets passed. That way we save memory, |
| 633 |
* by sponging off the label manager. |
| 634 |
*/ |
| 635 |
if (!name) { |
| 636 |
*bits = 16; |
| 637 |
if (!any_segs) |
| 638 |
return 0; |
| 639 |
return seghead->index; |
| 640 |
} else { |
| 641 |
struct ieeeSection *seg; |
| 642 |
int ieee_idx, attrs; |
| 643 |
bool rn_error; |
| 644 |
char *p; |
| 645 |
|
| 646 |
/* |
| 647 |
* Look for segment attributes. |
| 648 |
*/ |
| 649 |
attrs = 0; |
| 650 |
while (*name == '.') |
| 651 |
name++; /* hack, but a documented one */ |
| 652 |
p = name; |
| 653 |
while (*p && !nasm_isspace(*p)) |
| 654 |
p++; |
| 655 |
if (*p) { |
| 656 |
*p++ = '\0'; |
| 657 |
while (*p && nasm_isspace(*p)) |
| 658 |
*p++ = '\0'; |
| 659 |
} |
| 660 |
while (*p) { |
| 661 |
while (*p && !nasm_isspace(*p)) |
| 662 |
p++; |
| 663 |
if (*p) { |
| 664 |
*p++ = '\0'; |
| 665 |
while (*p && nasm_isspace(*p)) |
| 666 |
*p++ = '\0'; |
| 667 |
} |
| 668 |
|
| 669 |
attrs++; |
| 670 |
} |
| 671 |
|
| 672 |
ieee_idx = 1; |
| 673 |
for (seg = seghead; seg; seg = seg->next) { |
| 674 |
ieee_idx++; |
| 675 |
if (!strcmp(seg->name, name)) { |
| 676 |
if (attrs > 0 && seg->pass_last_seen == pass_count()) |
| 677 |
nasm_warn(WARN_OTHER, "segment attributes specified on" |
| 678 |
" redeclaration of segment: ignoring"); |
| 679 |
if (seg->use32) |
| 680 |
*bits = 32; |
| 681 |
else |
| 682 |
*bits = 16; |
| 683 |
|
| 684 |
seg->pass_last_seen = pass_count(); |
| 685 |
return seg->index; |
| 686 |
} |
| 687 |
} |
| 688 |
|
| 689 |
*segtail = seg = nasm_malloc(sizeof(*seg)); |
| 690 |
seg->next = NULL; |
| 691 |
segtail = &seg->next; |
| 692 |
seg->index = seg_alloc(); |
| 693 |
seg->ieee_index = ieee_idx; |
| 694 |
any_segs = true; |
| 695 |
seg->name = NULL; |
| 696 |
seg->currentpos = 0; |
| 697 |
seg->align = 1; /* default */ |
| 698 |
seg->use32 = *bits == 32; /* default to user spec */ |
| 699 |
seg->combine = CMB_PUBLIC; /* default */ |
| 700 |
seg->pubhead = NULL; |
| 701 |
seg->pubtail = &seg->pubhead; |
| 702 |
seg->data = NULL; |
| 703 |
seg->fptr = NULL; |
| 704 |
seg->lochead = NULL; |
| 705 |
seg->loctail = &seg->lochead; |
| 706 |
|
| 707 |
/* |
| 708 |
* Process the segment attributes. |
| 709 |
*/ |
| 710 |
p = name; |
| 711 |
while (attrs--) { |
| 712 |
p += strlen(p); |
| 713 |
while (!*p) |
| 714 |
p++; |
| 715 |
|
| 716 |
/* |
| 717 |
* `p' contains a segment attribute. |
| 718 |
*/ |
| 719 |
if (!nasm_stricmp(p, "private")) |
| 720 |
seg->combine = CMB_PRIVATE; |
| 721 |
else if (!nasm_stricmp(p, "public")) |
| 722 |
seg->combine = CMB_PUBLIC; |
| 723 |
else if (!nasm_stricmp(p, "common")) |
| 724 |
seg->combine = CMB_COMMON; |
| 725 |
else if (!nasm_stricmp(p, "use16")) |
| 726 |
seg->use32 = false; |
| 727 |
else if (!nasm_stricmp(p, "use32")) |
| 728 |
seg->use32 = true; |
| 729 |
else if (!nasm_strnicmp(p, "align=", 6)) { |
| 730 |
seg->align = readnum(p + 6, &rn_error); |
| 731 |
if (seg->align == 0) |
| 732 |
seg->align = 1; |
| 733 |
if (rn_error) { |
| 734 |
seg->align = 1; |
| 735 |
nasm_nonfatal("segment alignment should be numeric"); |
| 736 |
} |
| 737 |
switch (seg->align) { |
| 738 |
case 1: /* BYTE */ |
| 739 |
case 2: /* WORD */ |
| 740 |
case 4: /* DWORD */ |
| 741 |
case 16: /* PARA */ |
| 742 |
case 256: /* PAGE */ |
| 743 |
case 8: |
| 744 |
case 32: |
| 745 |
case 64: |
| 746 |
case 128: |
| 747 |
break; |
| 748 |
default: |
| 749 |
nasm_nonfatal("invalid alignment value %d", seg->align); |
| 750 |
seg->align = 1; |
| 751 |
break; |
| 752 |
} |
| 753 |
} else if (!nasm_strnicmp(p, "absolute=", 9)) { |
| 754 |
seg->align = SEG_ABS + readnum(p + 9, &rn_error); |
| 755 |
if (rn_error) |
| 756 |
nasm_nonfatal("argument to `absolute' segment" |
| 757 |
" attribute should be numeric"); |
| 758 |
} |
| 759 |
} |
| 760 |
|
| 761 |
ieee_seg_needs_update = seg; |
| 762 |
if (seg->align >= SEG_ABS) |
| 763 |
define_label(name, NO_SEG, seg->align - SEG_ABS, false); |
| 764 |
else |
| 765 |
define_label(name, seg->index + 1, 0L, false); |
| 766 |
ieee_seg_needs_update = NULL; |
| 767 |
|
| 768 |
/* |
| 769 |
* In commit 98578071b9d71ecaa2344dd9c185237c1765041e |
| 770 |
* we reworked labels significantly which in turn lead |
| 771 |
* to the case where seg->name = NULL here and we get |
| 772 |
* nil dereference in next segments definitions. |
| 773 |
* |
| 774 |
* Lets placate this case with explicit name setting |
| 775 |
* if labels engine didn't set it yet. |
| 776 |
* |
| 777 |
* FIXME: Need to revisit this moment if such fix doesn't |
| 778 |
* break anything but since IEEE 695 format is veeery |
| 779 |
* old I don't expect there are many users left. In worst |
| 780 |
* case this should only lead to a memory leak. |
| 781 |
*/ |
| 782 |
if (!seg->name) |
| 783 |
seg->name = nasm_strdup(name); |
| 784 |
|
| 785 |
if (seg->use32) |
| 786 |
*bits = 32; |
| 787 |
else |
| 788 |
*bits = 16; |
| 789 |
return seg->index; |
| 790 |
} |
| 791 |
} |
| 792 |
|
| 793 |
/* |
| 794 |
* directives supported |
| 795 |
*/ |
| 796 |
static enum directive_result |
| 797 |
ieee_directive(enum directive directive, char *value) |
| 798 |
{ |
| 799 |
(void)value; |
| 800 |
|
| 801 |
switch (directive) { |
| 802 |
case D_UPPERCASE: |
| 803 |
get_boolean_option(value, &ieee_uppercase); |
| 804 |
return DIRR_OK; |
| 805 |
|
| 806 |
default: |
| 807 |
return DIRR_UNKNOWN; |
| 808 |
} |
| 809 |
} |
| 810 |
|
| 811 |
static void ieee_sectalign(int32_t seg, unsigned int value) |
| 812 |
{ |
| 813 |
struct ieeeSection *s; |
| 814 |
|
| 815 |
list_for_each(s, seghead) { |
| 816 |
if (s->index == seg) |
| 817 |
break; |
| 818 |
} |
| 819 |
|
| 820 |
/* |
| 821 |
* 256 is maximum there, note it may happen |
| 822 |
* that align is issued on "absolute" segment |
| 823 |
* it's fine since SEG_ABS > 256 and we never |
| 824 |
* get escape this test |
| 825 |
*/ |
| 826 |
if (!s || !is_power2(value) || value > 256) |
| 827 |
return; |
| 828 |
|
| 829 |
if ((unsigned int)s->align < value) |
| 830 |
s->align = value; |
| 831 |
} |
| 832 |
|
| 833 |
/* |
| 834 |
* Return segment data |
| 835 |
*/ |
| 836 |
static int32_t ieee_segbase(int32_t segment) |
| 837 |
{ |
| 838 |
struct ieeeSection *seg; |
| 839 |
|
| 840 |
/* |
| 841 |
* Find the segment in our list. |
| 842 |
*/ |
| 843 |
for (seg = seghead; seg; seg = seg->next) |
| 844 |
if (seg->index == segment - 1) |
| 845 |
break; |
| 846 |
|
| 847 |
if (!seg) |
| 848 |
return segment; /* not one of ours - leave it alone */ |
| 849 |
|
| 850 |
if (seg->align >= SEG_ABS) |
| 851 |
return seg->align; /* absolute segment */ |
| 852 |
|
| 853 |
return segment; /* no special treatment */ |
| 854 |
} |
| 855 |
|
| 856 |
static void ieee_write_file(void) |
| 857 |
{ |
| 858 |
const struct tm * const thetime = &official_compile_time.local; |
| 859 |
struct FileName *fn; |
| 860 |
struct ieeeSection *seg; |
| 861 |
struct ieeePublic *pub, *loc; |
| 862 |
struct ieeeExternal *ext; |
| 863 |
struct ieeeObjData *data; |
| 864 |
struct ieeeFixupp *fix; |
| 865 |
struct Array *arr; |
| 866 |
int i; |
| 867 |
const bool debuginfo = (dfmt == &ladsoft_debug_form); |
| 868 |
|
| 869 |
/* |
| 870 |
* Write the module header |
| 871 |
*/ |
| 872 |
ieee_putascii("MBFNASM,%02X%s.\n", strlen(ieee_infile), ieee_infile); |
| 873 |
|
| 874 |
/* |
| 875 |
* Write the NASM boast comment. |
| 876 |
*/ |
| 877 |
ieee_putascii("CO0,%02X%s.\n", nasm_comment_len(), nasm_comment()); |
| 878 |
|
| 879 |
/* |
| 880 |
* write processor-specific information |
| 881 |
*/ |
| 882 |
ieee_putascii("AD8,4,L.\n"); |
| 883 |
|
| 884 |
/* |
| 885 |
* date and time |
| 886 |
*/ |
| 887 |
ieee_putascii("DT%04d%02d%02d%02d%02d%02d.\n", |
| 888 |
1900 + thetime->tm_year, thetime->tm_mon + 1, |
| 889 |
thetime->tm_mday, thetime->tm_hour, thetime->tm_min, |
| 890 |
thetime->tm_sec); |
| 891 |
/* |
| 892 |
* if debugging, dump file names |
| 893 |
*/ |
| 894 |
for (fn = fnhead; fn && debuginfo; fn = fn->next) { |
| 895 |
ieee_putascii("C0105,%02X%s.\n", strlen(fn->name), fn->name); |
| 896 |
} |
| 897 |
|
| 898 |
ieee_putascii("CO101,07ENDHEAD.\n"); |
| 899 |
/* |
| 900 |
* the standard doesn't specify when to put checksums, |
| 901 |
* we'll just do it periodically. |
| 902 |
*/ |
| 903 |
ieee_putcs(false); |
| 904 |
|
| 905 |
/* |
| 906 |
* Write the section headers |
| 907 |
*/ |
| 908 |
seg = seghead; |
| 909 |
if (!debuginfo && seg && !strcmp(seg->name, "??LINE")) |
| 910 |
seg = seg->next; |
| 911 |
while (seg) { |
| 912 |
char buf[256]; |
| 913 |
char attrib; |
| 914 |
switch (seg->combine) { |
| 915 |
case CMB_PUBLIC: |
| 916 |
default: |
| 917 |
attrib = 'C'; |
| 918 |
break; |
| 919 |
case CMB_PRIVATE: |
| 920 |
attrib = 'S'; |
| 921 |
break; |
| 922 |
case CMB_COMMON: |
| 923 |
attrib = 'M'; |
| 924 |
break; |
| 925 |
} |
| 926 |
ieee_unqualified_name(buf, seg->name); |
| 927 |
if (seg->align >= SEG_ABS) { |
| 928 |
ieee_putascii("ST%X,A,%02X%s.\n", seg->ieee_index, |
| 929 |
strlen(buf), buf); |
| 930 |
ieee_putascii("ASL%X,%lX.\n", seg->ieee_index, |
| 931 |
(seg->align - SEG_ABS) * 16); |
| 932 |
} else { |
| 933 |
ieee_putascii("ST%X,%c,%02X%s.\n", seg->ieee_index, attrib, |
| 934 |
strlen(buf), buf); |
| 935 |
ieee_putascii("SA%X,%lX.\n", seg->ieee_index, seg->align); |
| 936 |
ieee_putascii("ASS%X,%X.\n", seg->ieee_index, |
| 937 |
seg->currentpos); |
| 938 |
} |
| 939 |
seg = seg->next; |
| 940 |
} |
| 941 |
/* |
| 942 |
* write the start address if there is one |
| 943 |
*/ |
| 944 |
if (ieee_entry_seg && seghead) { |
| 945 |
for (seg = seghead; seg; seg = seg->next) |
| 946 |
if (seg->index == ieee_entry_seg) |
| 947 |
break; |
| 948 |
if (!seg) |
| 949 |
nasm_panic("Start address records are incorrect"); |
| 950 |
else |
| 951 |
ieee_putascii("ASG,R%X,%lX,+.\n", seg->ieee_index, |
| 952 |
ieee_entry_ofs); |
| 953 |
} |
| 954 |
|
| 955 |
ieee_putcs(false); |
| 956 |
/* |
| 957 |
* Write the publics |
| 958 |
*/ |
| 959 |
i = 1; |
| 960 |
for (seg = seghead; seg; seg = seg->next) { |
| 961 |
for (pub = seg->pubhead; pub; pub = pub->next) { |
| 962 |
char buf[256]; |
| 963 |
ieee_unqualified_name(buf, pub->name); |
| 964 |
ieee_putascii("NI%X,%02X%s.\n", i, strlen(buf), buf); |
| 965 |
if (pub->segment == -1) |
| 966 |
ieee_putascii("ASI%X,R%X,%lX,+.\n", i, pub->index, |
| 967 |
pub->offset); |
| 968 |
else |
| 969 |
ieee_putascii("ASI%X,%lX,%lX,+.\n", i, pub->segment * 16, |
| 970 |
pub->offset); |
| 971 |
if (debuginfo) { |
| 972 |
if (pub->type >= 0x100) |
| 973 |
ieee_putascii("ATI%X,T%X.\n", i, pub->type - 0x100); |
| 974 |
else |
| 975 |
ieee_putascii("ATI%X,%X.\n", i, pub->type); |
| 976 |
} |
| 977 |
i++; |
| 978 |
} |
| 979 |
} |
| 980 |
pub = fpubhead; |
| 981 |
i = 1; |
| 982 |
while (pub) { |
| 983 |
char buf[256]; |
| 984 |
ieee_unqualified_name(buf, pub->name); |
| 985 |
ieee_putascii("NI%X,%02X%s.\n", i, strlen(buf), buf); |
| 986 |
if (pub->segment == -1) |
| 987 |
ieee_putascii("ASI%X,R%X,%lX,+.\n", i, pub->index, |
| 988 |
pub->offset); |
| 989 |
else |
| 990 |
ieee_putascii("ASI%X,%lX,%lX,+.\n", i, pub->segment * 16, |
| 991 |
pub->offset); |
| 992 |
if (debuginfo) { |
| 993 |
if (pub->type >= 0x100) |
| 994 |
ieee_putascii("ATI%X,T%X.\n", i, pub->type - 0x100); |
| 995 |
else |
| 996 |
ieee_putascii("ATI%X,%X.\n", i, pub->type); |
| 997 |
} |
| 998 |
i++; |
| 999 |
pub = pub->next; |
| 1000 |
} |
| 1001 |
/* |
| 1002 |
* Write the externals |
| 1003 |
*/ |
| 1004 |
ext = exthead; |
| 1005 |
i = 1; |
| 1006 |
while (ext) { |
| 1007 |
char buf[256]; |
| 1008 |
ieee_unqualified_name(buf, ext->name); |
| 1009 |
ieee_putascii("NX%X,%02X%s.\n", i++, strlen(buf), buf); |
| 1010 |
ext = ext->next; |
| 1011 |
} |
| 1012 |
ieee_putcs(false); |
| 1013 |
|
| 1014 |
/* |
| 1015 |
* IEEE doesn't have a standard pass break record |
| 1016 |
* so use the ladsoft variant |
| 1017 |
*/ |
| 1018 |
ieee_putascii("CO100,06ENDSYM.\n"); |
| 1019 |
|
| 1020 |
/* |
| 1021 |
* now put types |
| 1022 |
*/ |
| 1023 |
i = ARRAY_BOT; |
| 1024 |
for (arr = arrhead; arr && debuginfo; arr = arr->next) { |
| 1025 |
ieee_putascii("TY%X,20,%X,%lX.\n", i++, arr->basetype, |
| 1026 |
arr->size); |
| 1027 |
} |
| 1028 |
/* |
| 1029 |
* now put locals |
| 1030 |
*/ |
| 1031 |
i = 1; |
| 1032 |
for (seg = seghead; seg && debuginfo; seg = seg->next) { |
| 1033 |
for (loc = seg->lochead; loc; loc = loc->next) { |
| 1034 |
char buf[256]; |
| 1035 |
ieee_unqualified_name(buf, loc->name); |
| 1036 |
ieee_putascii("NN%X,%02X%s.\n", i, strlen(buf), buf); |
| 1037 |
if (loc->segment == -1) |
| 1038 |
ieee_putascii("ASN%X,R%X,%lX,+.\n", i, loc->index, |
| 1039 |
loc->offset); |
| 1040 |
else |
| 1041 |
ieee_putascii("ASN%X,%lX,%lX,+.\n", i, loc->segment * 16, |
| 1042 |
loc->offset); |
| 1043 |
if (debuginfo) { |
| 1044 |
if (loc->type >= 0x100) |
| 1045 |
ieee_putascii("ATN%X,T%X.\n", i, loc->type - 0x100); |
| 1046 |
else |
| 1047 |
ieee_putascii("ATN%X,%X.\n", i, loc->type); |
| 1048 |
} |
| 1049 |
i++; |
| 1050 |
} |
| 1051 |
} |
| 1052 |
|
| 1053 |
/* |
| 1054 |
* put out section data; |
| 1055 |
*/ |
| 1056 |
seg = seghead; |
| 1057 |
if (!debuginfo && seg && !strcmp(seg->name, "??LINE")) |
| 1058 |
seg = seg->next; |
| 1059 |
while (seg) { |
| 1060 |
if (seg->currentpos) { |
| 1061 |
int32_t size, org = 0; |
| 1062 |
data = seg->data; |
| 1063 |
ieee_putascii("SB%X.\n", seg->ieee_index); |
| 1064 |
fix = seg->fptr; |
| 1065 |
while (fix) { |
| 1066 |
size = HUNKSIZE - (org % HUNKSIZE); |
| 1067 |
size = |
| 1068 |
size + org > |
| 1069 |
seg->currentpos ? seg->currentpos - org : size; |
| 1070 |
size = fix->offset - org > size ? size : fix->offset - org; |
| 1071 |
org = ieee_putld(org, org + size, data->data); |
| 1072 |
if (org % HUNKSIZE == 0) |
| 1073 |
data = data->next; |
| 1074 |
if (org == fix->offset) { |
| 1075 |
org += ieee_putlr(fix); |
| 1076 |
fix = fix->next; |
| 1077 |
} |
| 1078 |
} |
| 1079 |
while (org < seg->currentpos && data) { |
| 1080 |
size = |
| 1081 |
seg->currentpos - org > |
| 1082 |
HUNKSIZE ? HUNKSIZE : seg->currentpos - org; |
| 1083 |
org = ieee_putld(org, org + size, data->data); |
| 1084 |
data = data->next; |
| 1085 |
} |
| 1086 |
ieee_putcs(false); |
| 1087 |
|
| 1088 |
} |
| 1089 |
seg = seg->next; |
| 1090 |
} |
| 1091 |
/* |
| 1092 |
* module end record |
| 1093 |
*/ |
| 1094 |
ieee_putascii("ME.\n"); |
| 1095 |
} |
| 1096 |
|
| 1097 |
static void ieee_write_byte(struct ieeeSection *seg, int data) |
| 1098 |
{ |
| 1099 |
int temp; |
| 1100 |
if (!(temp = seg->currentpos++ % HUNKSIZE)) |
| 1101 |
ieee_data_new(seg); |
| 1102 |
seg->datacurr->data[temp] = data; |
| 1103 |
} |
| 1104 |
|
| 1105 |
static void ieee_write_word(struct ieeeSection *seg, int data) |
| 1106 |
{ |
| 1107 |
ieee_write_byte(seg, data & 0xFF); |
| 1108 |
ieee_write_byte(seg, (data >> 8) & 0xFF); |
| 1109 |
} |
| 1110 |
|
| 1111 |
static void ieee_write_dword(struct ieeeSection *seg, int32_t data) |
| 1112 |
{ |
| 1113 |
ieee_write_byte(seg, data & 0xFF); |
| 1114 |
ieee_write_byte(seg, (data >> 8) & 0xFF); |
| 1115 |
ieee_write_byte(seg, (data >> 16) & 0xFF); |
| 1116 |
ieee_write_byte(seg, (data >> 24) & 0xFF); |
| 1117 |
} |
| 1118 |
static void printf_func(1, 2) ieee_putascii(char *format, ...) |
| 1119 |
{ |
| 1120 |
char buffer[256]; |
| 1121 |
size_t i, l; |
| 1122 |
va_list ap; |
| 1123 |
|
| 1124 |
va_start(ap, format); |
| 1125 |
l = vsnprintf(buffer, sizeof(buffer), format, ap); |
| 1126 |
nasm_assert(l < sizeof(buffer)); |
| 1127 |
for (i = 0; i < l; i++) |
| 1128 |
if ((uint8_t)buffer[i] > 31) |
| 1129 |
checksum += buffer[i]; |
| 1130 |
va_end(ap); |
| 1131 |
fputs(buffer, ofile); |
| 1132 |
} |
| 1133 |
|
| 1134 |
/* |
| 1135 |
* put out a checksum record */ |
| 1136 |
static void ieee_putcs(int toclear) |
| 1137 |
{ |
| 1138 |
if (toclear) { |
| 1139 |
ieee_putascii("CS.\n"); |
| 1140 |
} else { |
| 1141 |
checksum += 'C'; |
| 1142 |
checksum += 'S'; |
| 1143 |
ieee_putascii("CS%02X.\n", checksum & 127); |
| 1144 |
} |
| 1145 |
checksum = 0; |
| 1146 |
} |
| 1147 |
|
| 1148 |
static int32_t ieee_putld(int32_t start, int32_t end, uint8_t *buf) |
| 1149 |
{ |
| 1150 |
int32_t val; |
| 1151 |
if (start == end) |
| 1152 |
return (start); |
| 1153 |
val = start % HUNKSIZE; |
| 1154 |
/* fill up multiple lines */ |
| 1155 |
while (end - start >= LDPERLINE) { |
| 1156 |
int i; |
| 1157 |
ieee_putascii("LD"); |
| 1158 |
for (i = 0; i < LDPERLINE; i++) { |
| 1159 |
ieee_putascii("%02X", buf[val++]); |
| 1160 |
start++; |
| 1161 |
} |
| 1162 |
ieee_putascii(".\n"); |
| 1163 |
} |
| 1164 |
/* if no partial lines */ |
| 1165 |
if (start == end) |
| 1166 |
return (start); |
| 1167 |
/* make a partial line */ |
| 1168 |
ieee_putascii("LD"); |
| 1169 |
while (start < end) { |
| 1170 |
ieee_putascii("%02X", buf[val++]); |
| 1171 |
start++; |
| 1172 |
} |
| 1173 |
ieee_putascii(".\n"); |
| 1174 |
return start; |
| 1175 |
} |
| 1176 |
static int32_t ieee_putlr(struct ieeeFixupp *p) |
| 1177 |
{ |
| 1178 |
/* |
| 1179 |
* To deal with the vagaries of segmentation the LADsoft linker |
| 1180 |
* defines two types of segments: absolute and virtual. Note that |
| 1181 |
* 'absolute' in this context is a different thing from the IEEE |
| 1182 |
* definition of an absolute segment type, which is also supported. If a |
| 1183 |
* segment is linked in virtual mode the low limit (L-var) is |
| 1184 |
* subtracted from each R,X, and P variable which appears in an |
| 1185 |
* expression, so that we can have relative offsets. Meanwhile |
| 1186 |
* in the ABSOLUTE mode this subtraction is not done and |
| 1187 |
* so we can use absolute offsets from 0. In the LADsoft linker |
| 1188 |
* this configuration is not done in the assemblker source but in |
| 1189 |
* a source the linker reads. Generally this type of thing only |
| 1190 |
* becomes an issue if real mode code is used. A pure 32-bit linker could |
| 1191 |
* get away without defining the virtual mode... |
| 1192 |
*/ |
| 1193 |
char buf[40]; |
| 1194 |
int32_t size = p->size; |
| 1195 |
switch (p->ftype) { |
| 1196 |
case FT_SEG: |
| 1197 |
if (p->id1 < 0) |
| 1198 |
sprintf(buf, "%"PRIX32"", -p->id1); |
| 1199 |
else |
| 1200 |
sprintf(buf, "L%"PRIX32",10,/", p->id1); |
| 1201 |
break; |
| 1202 |
case FT_OFS: |
| 1203 |
sprintf(buf, "R%"PRIX32",%"PRIX32",+", p->id1, p->addend); |
| 1204 |
break; |
| 1205 |
case FT_REL: |
| 1206 |
sprintf(buf, "R%"PRIX32",%"PRIX32",+,P,-,%X,-", p->id1, p->addend, p->size); |
| 1207 |
break; |
| 1208 |
|
| 1209 |
case FT_WRT: |
| 1210 |
if (p->id2 < 0) |
| 1211 |
sprintf(buf, "R%"PRIX32",%"PRIX32",+,L%"PRIX32",+,%"PRIX32",-", p->id2, p->addend, |
| 1212 |
p->id2, -p->id1 * 16); |
| 1213 |
else |
| 1214 |
sprintf(buf, "R%"PRIX32",%"PRIX32",+,L%"PRIX32",+,L%"PRIX32",-", p->id2, p->addend, |
| 1215 |
p->id2, p->id1); |
| 1216 |
break; |
| 1217 |
case FT_EXT: |
| 1218 |
sprintf(buf, "X%"PRIX32"", p->id1); |
| 1219 |
break; |
| 1220 |
case FT_EXTREL: |
| 1221 |
sprintf(buf, "X%"PRIX32",P,-,%"PRIX32",-", p->id1, size); |
| 1222 |
break; |
| 1223 |
case FT_EXTSEG: |
| 1224 |
/* We needed a non-ieee hack here. |
| 1225 |
* We introduce the Y variable, which is the low |
| 1226 |
* limit of the native segment the extern resides in |
| 1227 |
*/ |
| 1228 |
sprintf(buf, "Y%"PRIX32",10,/", p->id1); |
| 1229 |
break; |
| 1230 |
case FT_EXTWRT: |
| 1231 |
if (p->id2 < 0) |
| 1232 |
sprintf(buf, "X%"PRIX32",Y%"PRIX32",+,%"PRIX32",-", p->id2, p->id2, |
| 1233 |
-p->id1 * 16); |
| 1234 |
else |
| 1235 |
sprintf(buf, "X%"PRIX32",Y%"PRIX32",+,L%"PRIX32",-", p->id2, p->id2, p->id1); |
| 1236 |
break; |
| 1237 |
} |
| 1238 |
ieee_putascii("LR(%s,%"PRIX32").\n", buf, size); |
| 1239 |
|
| 1240 |
return size; |
| 1241 |
} |
| 1242 |
|
| 1243 |
/* Dump all segment data (text and fixups )*/ |
| 1244 |
|
| 1245 |
static void ieee_unqualified_name(char *dest, char *source) |
| 1246 |
{ |
| 1247 |
if (ieee_uppercase) { |
| 1248 |
while (*source) |
| 1249 |
*dest++ = toupper(*source++); |
| 1250 |
*dest = 0; |
| 1251 |
} else |
| 1252 |
strcpy(dest, source); |
| 1253 |
} |
| 1254 |
static void dbgls_init(void) |
| 1255 |
{ |
| 1256 |
int tempint; |
| 1257 |
|
| 1258 |
fnhead = NULL; |
| 1259 |
fntail = &fnhead; |
| 1260 |
arrindex = ARRAY_BOT; |
| 1261 |
arrhead = NULL; |
| 1262 |
arrtail = &arrhead; |
| 1263 |
ieee_segment("??LINE", &tempint); |
| 1264 |
any_segs = false; |
| 1265 |
} |
| 1266 |
static void dbgls_cleanup(void) |
| 1267 |
{ |
| 1268 |
struct ieeeSection *segtmp; |
| 1269 |
while (fnhead) { |
| 1270 |
struct FileName *fntemp = fnhead; |
| 1271 |
fnhead = fnhead->next; |
| 1272 |
nasm_free(fntemp->name); |
| 1273 |
nasm_free(fntemp); |
| 1274 |
} |
| 1275 |
for (segtmp = seghead; segtmp; segtmp = segtmp->next) { |
| 1276 |
while (segtmp->lochead) { |
| 1277 |
struct ieeePublic *loctmp = segtmp->lochead; |
| 1278 |
segtmp->lochead = loctmp->next; |
| 1279 |
nasm_free(loctmp->name); |
| 1280 |
nasm_free(loctmp); |
| 1281 |
} |
| 1282 |
} |
| 1283 |
while (arrhead) { |
| 1284 |
struct Array *arrtmp = arrhead; |
| 1285 |
arrhead = arrhead->next; |
| 1286 |
nasm_free(arrtmp); |
| 1287 |
} |
| 1288 |
} |
| 1289 |
|
| 1290 |
/* |
| 1291 |
* because this routine is not bracketed in |
| 1292 |
* the main program, this routine will be called even if there |
| 1293 |
* is no request for debug info |
| 1294 |
* so, we have to make sure the ??LINE segment is available |
| 1295 |
* as the first segment when this debug format is selected |
| 1296 |
*/ |
| 1297 |
static void dbgls_linnum(const char *lnfname, int32_t lineno, int32_t segto) |
| 1298 |
{ |
| 1299 |
struct FileName *fn; |
| 1300 |
struct ieeeSection *seg; |
| 1301 |
int i = 0; |
| 1302 |
if (segto == NO_SEG) |
| 1303 |
return; |
| 1304 |
|
| 1305 |
/* |
| 1306 |
* If `any_segs' is still false, we must define a default |
| 1307 |
* segment. |
| 1308 |
*/ |
| 1309 |
if (!any_segs) { |
| 1310 |
int tempint; /* ignored */ |
| 1311 |
if (segto != ieee_segment("__NASMDEFSEG", &tempint)) |
| 1312 |
nasm_panic("strange segment conditions in IEEE driver"); |
| 1313 |
} |
| 1314 |
|
| 1315 |
/* |
| 1316 |
* Find the segment we are targeting. |
| 1317 |
*/ |
| 1318 |
for (seg = seghead; seg; seg = seg->next) |
| 1319 |
if (seg->index == segto) |
| 1320 |
break; |
| 1321 |
if (!seg) |
| 1322 |
nasm_panic("lineno directed to nonexistent segment?"); |
| 1323 |
|
| 1324 |
for (fn = fnhead; fn; fn = fn->next) { |
| 1325 |
if (!nasm_stricmp(lnfname, fn->name)) |
| 1326 |
break; |
| 1327 |
i++; |
| 1328 |
} |
| 1329 |
if (!fn) { |
| 1330 |
fn = nasm_malloc(sizeof(*fn)); |
| 1331 |
fn->name = nasm_malloc(strlen(lnfname) + 1); |
| 1332 |
fn->index = i; |
| 1333 |
strcpy(fn->name, lnfname); |
| 1334 |
fn->next = NULL; |
| 1335 |
*fntail = fn; |
| 1336 |
fntail = &fn->next; |
| 1337 |
} |
| 1338 |
ieee_write_byte(seghead, fn->index); |
| 1339 |
ieee_write_word(seghead, lineno); |
| 1340 |
ieee_write_fixup(segto, NO_SEG, seghead, 4, OUT_ADDRESS, |
| 1341 |
seg->currentpos); |
| 1342 |
|
| 1343 |
} |
| 1344 |
static void dbgls_deflabel(char *name, int32_t segment, |
| 1345 |
int64_t offset, int is_global, char *special) |
| 1346 |
{ |
| 1347 |
struct ieeeSection *seg; |
| 1348 |
|
| 1349 |
/* Keep compiler from warning about special */ |
| 1350 |
(void)special; |
| 1351 |
|
| 1352 |
/* |
| 1353 |
* Note: ..[^@] special symbols are filtered in labels.c |
| 1354 |
*/ |
| 1355 |
|
| 1356 |
/* |
| 1357 |
* If it's a special-retry from pass two, discard it. |
| 1358 |
*/ |
| 1359 |
if (is_global == 3) |
| 1360 |
return; |
| 1361 |
|
| 1362 |
/* |
| 1363 |
* Case (i): |
| 1364 |
*/ |
| 1365 |
if (ieee_seg_needs_update) |
| 1366 |
return; |
| 1367 |
if (segment < SEG_ABS && segment != NO_SEG && segment % 2) |
| 1368 |
return; |
| 1369 |
|
| 1370 |
if (segment >= SEG_ABS || segment == NO_SEG) { |
| 1371 |
return; |
| 1372 |
} |
| 1373 |
|
| 1374 |
/* |
| 1375 |
* If `any_segs' is still false, we might need to define a |
| 1376 |
* default segment, if they're trying to declare a label in |
| 1377 |
* `first_seg'. But the label should exist due to a prior |
| 1378 |
* call to ieee_deflabel so we can skip that. |
| 1379 |
*/ |
| 1380 |
|
| 1381 |
for (seg = seghead; seg; seg = seg->next) |
| 1382 |
if (seg->index == segment) { |
| 1383 |
struct ieeePublic *loc; |
| 1384 |
/* |
| 1385 |
* Case (ii). Maybe MODPUB someday? |
| 1386 |
*/ |
| 1387 |
if (!is_global) { |
| 1388 |
last_defined = loc = nasm_malloc(sizeof(*loc)); |
| 1389 |
*seg->loctail = loc; |
| 1390 |
seg->loctail = &loc->next; |
| 1391 |
loc->next = NULL; |
| 1392 |
loc->name = nasm_strdup(name); |
| 1393 |
loc->offset = offset; |
| 1394 |
loc->segment = -1; |
| 1395 |
loc->index = seg->ieee_index; |
| 1396 |
} |
| 1397 |
} |
| 1398 |
} |
| 1399 |
static void dbgls_typevalue(int32_t type) |
| 1400 |
{ |
| 1401 |
int elem = TYM_ELEMENTS(type); |
| 1402 |
type = TYM_TYPE(type); |
| 1403 |
|
| 1404 |
if (!last_defined) |
| 1405 |
return; |
| 1406 |
|
| 1407 |
switch (type) { |
| 1408 |
case TY_BYTE: |
| 1409 |
last_defined->type = 1; /* uint8_t */ |
| 1410 |
break; |
| 1411 |
case TY_WORD: |
| 1412 |
last_defined->type = 3; /* unsigned word */ |
| 1413 |
break; |
| 1414 |
case TY_DWORD: |
| 1415 |
last_defined->type = 5; /* unsigned dword */ |
| 1416 |
break; |
| 1417 |
case TY_FLOAT: |
| 1418 |
last_defined->type = 9; /* float */ |
| 1419 |
break; |
| 1420 |
case TY_QWORD: |
| 1421 |
last_defined->type = 10; /* qword */ |
| 1422 |
break; |
| 1423 |
case TY_TBYTE: |
| 1424 |
last_defined->type = 11; /* TBYTE */ |
| 1425 |
break; |
| 1426 |
default: |
| 1427 |
last_defined->type = 0x10; /* near label */ |
| 1428 |
break; |
| 1429 |
} |
| 1430 |
|
| 1431 |
if (elem > 1) { |
| 1432 |
struct Array *arrtmp = nasm_malloc(sizeof(*arrtmp)); |
| 1433 |
int vtype = last_defined->type; |
| 1434 |
arrtmp->size = elem; |
| 1435 |
arrtmp->basetype = vtype; |
| 1436 |
arrtmp->next = NULL; |
| 1437 |
last_defined->type = arrindex++ + 0x100; |
| 1438 |
*arrtail = arrtmp; |
| 1439 |
arrtail = &(arrtmp->next); |
| 1440 |
} |
| 1441 |
last_defined = NULL; |
| 1442 |
} |
| 1443 |
static void dbgls_output(int output_type, void *param) |
| 1444 |
{ |
| 1445 |
(void)output_type; |
| 1446 |
(void)param; |
| 1447 |
} |
| 1448 |
static const struct dfmt ladsoft_debug_form = { |
| 1449 |
"LADsoft Debug Records", |
| 1450 |
"ladsoft", |
| 1451 |
dbgls_init, |
| 1452 |
dbgls_linnum, |
| 1453 |
dbgls_deflabel, |
| 1454 |
NULL, /* .debug_smacros */ |
| 1455 |
NULL, /* .debug_include */ |
| 1456 |
NULL, /* .debug_mmacros */ |
| 1457 |
null_debug_directive, |
| 1458 |
dbgls_typevalue, |
| 1459 |
dbgls_output, |
| 1460 |
dbgls_cleanup, |
| 1461 |
NULL /* pragma list */ |
| 1462 |
}; |
| 1463 |
static const struct dfmt * const ladsoft_debug_arr[3] = { |
| 1464 |
&ladsoft_debug_form, |
| 1465 |
&null_debug_form, |
| 1466 |
NULL |
| 1467 |
}; |
| 1468 |
const struct ofmt of_ieee = { |
| 1469 |
"IEEE-695 (LADsoft variant) object file format", |
| 1470 |
"ieee", |
| 1471 |
".o", |
| 1472 |
OFMT_TEXT, |
| 1473 |
32, |
| 1474 |
ladsoft_debug_arr, |
| 1475 |
&ladsoft_debug_form, |
| 1476 |
NULL, |
| 1477 |
ieee_init, |
| 1478 |
null_reset, |
| 1479 |
ieee_out, |
| 1480 |
ieee_deflabel, |
| 1481 |
ieee_segment, |
| 1482 |
NULL, |
| 1483 |
ieee_sectalign, |
| 1484 |
ieee_segbase, |
| 1485 |
ieee_directive, |
| 1486 |
ieee_cleanup, |
| 1487 |
NULL /* pragma list */ |
| 1488 |
}; |
| 1489 |
|
| 1490 |
#endif /* OF_IEEE */ |