| 1 |
/* SPDX-License-Identifier: BSD-2-Clause */ |
| 2 |
/* Copyright 1996-2025 The NASM Authors - All Rights Reserved */ |
| 3 |
|
| 4 |
/* |
| 5 |
* outobj.c output routines for the Netwide Assembler to produce |
| 6 |
* .OBJ object files |
| 7 |
*/ |
| 8 |
|
| 9 |
#include "compiler.h" |
| 10 |
|
| 11 |
#include <ctype.h> /* For toupper() */ |
| 12 |
#include "nctype.h" |
| 13 |
|
| 14 |
#include "nasm.h" |
| 15 |
#include "nasmlib.h" |
| 16 |
#include "asmutil.h" |
| 17 |
#include "error.h" |
| 18 |
#include "stdscan.h" |
| 19 |
#include "eval.h" |
| 20 |
#include "ver.h" |
| 21 |
|
| 22 |
#include "outform.h" |
| 23 |
#include "outlib.h" |
| 24 |
|
| 25 |
#if defined(OF_OBJ) || defined(OF_OBJ2) |
| 26 |
|
| 27 |
/* |
| 28 |
* outobj.c is divided into two sections. The first section is low level |
| 29 |
* routines for creating obj records; It has nearly zero NASM specific |
| 30 |
* code. The second section is high level routines for processing calls and |
| 31 |
* data structures from the rest of NASM into obj format. |
| 32 |
* |
| 33 |
* It should be easy (though not zero work) to lift the first section out for |
| 34 |
* use as an obj file writer for some other assembler or compiler. |
| 35 |
*/ |
| 36 |
|
| 37 |
/* |
| 38 |
* These routines are built around the ObjRecord data structure. An ObjRecord |
| 39 |
* holds an object file record that may be under construction or complete. |
| 40 |
* |
| 41 |
* A major function of these routines is to support continuation of an obj |
| 42 |
* record into the next record when the maximum record size is exceeded. The |
| 43 |
* high level code does not need to worry about where the record breaks occur. |
| 44 |
* It does need to do some minor extra steps to make the automatic continuation |
| 45 |
* work. Those steps may be skipped for records where the high level knows no |
| 46 |
* continuation could be required. |
| 47 |
* |
| 48 |
* 1) An ObjRecord is allocated and cleared by obj_new, or an existing ObjRecord |
| 49 |
* is cleared by obj_clear. |
| 50 |
* |
| 51 |
* 2) The caller should fill in .type. |
| 52 |
* |
| 53 |
* 3) If the record is continuable and there is processing that must be done at |
| 54 |
* the start of each record then the caller should fill in .ori with the |
| 55 |
* address of the record initializer routine. |
| 56 |
* |
| 57 |
* 4) If the record is continuable and it should be saved (rather than emitted |
| 58 |
* immediately) as each record is done, the caller should set .up to be a |
| 59 |
* pointer to a location in which the caller keeps the master pointer to the |
| 60 |
* ObjRecord. When the record is continued, the obj_bump routine will then |
| 61 |
* allocate a new ObjRecord structure and update the master pointer. |
| 62 |
* |
| 63 |
* 5) If the .ori field was used then the caller should fill in the .parm with |
| 64 |
* any data required by the initializer. |
| 65 |
* |
| 66 |
* 6) The caller uses the routines: obj_byte, obj_word, obj_rword, obj_dword, |
| 67 |
* obj_x, obj_index, obj_value and obj_name to fill in the various kinds of |
| 68 |
* data required for this record. |
| 69 |
* |
| 70 |
* 7) If the record is continuable, the caller should call obj_commit at each |
| 71 |
* point where breaking the record is permitted. |
| 72 |
* |
| 73 |
* 8) To write out the record, the caller should call obj_emit2. If the |
| 74 |
* caller has called obj_commit for all data written then he can get slightly |
| 75 |
* faster code by calling obj_emit instead of obj_emit2. |
| 76 |
* |
| 77 |
* Most of these routines return an ObjRecord pointer. This will be the input |
| 78 |
* pointer most of the time and will be the new location if the ObjRecord |
| 79 |
* moved as a result of the call. The caller may ignore the return value in |
| 80 |
* three cases: It is a "Never Reallocates" routine; or The caller knows |
| 81 |
* continuation is not possible; or The caller uses the master pointer for the |
| 82 |
* next operation. |
| 83 |
*/ |
| 84 |
|
| 85 |
#define RECORD_MAX (1024-3) /* maximal size of any record except type+reclen */ |
| 86 |
#define OBJ_PARMS 3 /* maximum .parm used by any .ori routine */ |
| 87 |
|
| 88 |
#define FIX_08_LOW 0x8000 /* location type for various fixup subrecords */ |
| 89 |
#define FIX_16_OFFSET 0x8400 |
| 90 |
#define FIX_16_SELECTOR 0x8800 |
| 91 |
#define FIX_32_POINTER 0x8C00 |
| 92 |
#define FIX_08_HIGH 0x9000 |
| 93 |
#define FIX_32_OFFSET 0xA400 |
| 94 |
#define FIX_48_POINTER 0xAC00 |
| 95 |
|
| 96 |
enum RecordID { /* record ID codes */ |
| 97 |
|
| 98 |
THEADR = 0x80, /* module header */ |
| 99 |
COMENT = 0x88, /* comment record */ |
| 100 |
|
| 101 |
LINNUM = 0x94, /* line number record */ |
| 102 |
LNAMES = 0x96, /* list of names */ |
| 103 |
|
| 104 |
SEGDEF = 0x98, /* segment definition */ |
| 105 |
GRPDEF = 0x9A, /* group definition */ |
| 106 |
EXTDEF = 0x8C, /* external definition */ |
| 107 |
PUBDEF = 0x90, /* public definition */ |
| 108 |
COMDEF = 0xB0, /* common definition */ |
| 109 |
|
| 110 |
LEDATA = 0xA0, /* logical enumerated data */ |
| 111 |
FIXUPP = 0x9C, /* fixups (relocations) */ |
| 112 |
FIXU32 = 0x9D, /* 32-bit fixups (relocations) */ |
| 113 |
|
| 114 |
MODEND = 0x8A, /* module end */ |
| 115 |
MODE32 = 0x8B /* module end for 32-bit objects */ |
| 116 |
}; |
| 117 |
|
| 118 |
enum ComentID { /* ID codes for comment records */ |
| 119 |
dTRANSL = 0x0000, /* translator comment */ |
| 120 |
dOMFEXT = 0xC0A0, /* "OMF extension" */ |
| 121 |
dEXTENDED = 0xC0A1, /* translator-specific extensions */ |
| 122 |
dLINKPASS = 0x40A2, /* link pass 2 marker */ |
| 123 |
dTYPEDEF = 0xC0E3, /* define a type */ |
| 124 |
dSYM = 0xC0E6, /* symbol debug record */ |
| 125 |
dFILNAME = 0xC0E8, /* file name record */ |
| 126 |
dDEPFILE = 0xC0E9, /* dependency file */ |
| 127 |
dCOMPDEF = 0xC0EA /* compiler type info */ |
| 128 |
}; |
| 129 |
|
| 130 |
typedef struct ObjRecord ObjRecord; |
| 131 |
typedef void ORI(ObjRecord * orp); |
| 132 |
|
| 133 |
struct ObjRecord { |
| 134 |
ORI *ori; /* Initialization routine */ |
| 135 |
int used; /* Current data size */ |
| 136 |
int committed; /* Data size at last boundary */ |
| 137 |
int x_size; /* (see obj_x) */ |
| 138 |
unsigned int type; /* Record type */ |
| 139 |
ObjRecord *child; /* Associated record below this one */ |
| 140 |
ObjRecord **up; /* Master pointer to this ObjRecord */ |
| 141 |
ObjRecord *back; /* Previous part of this record */ |
| 142 |
uint32_t parm[OBJ_PARMS]; /* Parameters for ori routine */ |
| 143 |
uint8_t buf[RECORD_MAX + 3]; |
| 144 |
}; |
| 145 |
|
| 146 |
static void obj_fwrite(ObjRecord * orp); |
| 147 |
static void ori_ledata(ObjRecord * orp); |
| 148 |
static void ori_pubdef(ObjRecord * orp); |
| 149 |
static void ori_null(ObjRecord * orp); |
| 150 |
static ObjRecord *obj_commit(ObjRecord * orp); |
| 151 |
|
| 152 |
static bool obj_uppercase; /* Flag: all names in uppercase */ |
| 153 |
static bool obj_use32; /* Flag: at least one segment is 32-bit */ |
| 154 |
static bool obj_nodepend; /* Flag: don't emit file dependencies */ |
| 155 |
|
| 156 |
/* |
| 157 |
* Clear an ObjRecord structure. (Never reallocates). |
| 158 |
* To simplify reuse of ObjRecord's, .type, .ori and .parm are not cleared. |
| 159 |
*/ |
| 160 |
static ObjRecord *obj_clear(ObjRecord * orp) |
| 161 |
{ |
| 162 |
orp->used = 0; |
| 163 |
orp->committed = 0; |
| 164 |
orp->x_size = 0; |
| 165 |
orp->child = NULL; |
| 166 |
orp->up = NULL; |
| 167 |
orp->back = NULL; |
| 168 |
return orp; |
| 169 |
} |
| 170 |
|
| 171 |
/* |
| 172 |
* Emit an ObjRecord structure. (Never reallocates). |
| 173 |
* The record is written out proceeded (recursively) by its previous part (if |
| 174 |
* any) and followed (recursively) by its child (if any). |
| 175 |
* The previous part and the child are freed. The main ObjRecord is cleared, |
| 176 |
* not freed. |
| 177 |
*/ |
| 178 |
static ObjRecord *obj_emit(ObjRecord * orp) |
| 179 |
{ |
| 180 |
if (orp->back) { |
| 181 |
obj_emit(orp->back); |
| 182 |
nasm_free(orp->back); |
| 183 |
} |
| 184 |
|
| 185 |
if (orp->committed) |
| 186 |
obj_fwrite(orp); |
| 187 |
|
| 188 |
if (orp->child) { |
| 189 |
obj_emit(orp->child); |
| 190 |
nasm_free(orp->child); |
| 191 |
} |
| 192 |
|
| 193 |
return obj_clear(orp); |
| 194 |
} |
| 195 |
|
| 196 |
/* |
| 197 |
* Commit and Emit a record. (Never reallocates). |
| 198 |
*/ |
| 199 |
static ObjRecord *obj_emit2(ObjRecord * orp) |
| 200 |
{ |
| 201 |
obj_commit(orp); |
| 202 |
return obj_emit(orp); |
| 203 |
} |
| 204 |
|
| 205 |
/* |
| 206 |
* Allocate and clear a new ObjRecord; Also sets .ori to ori_null |
| 207 |
*/ |
| 208 |
static ObjRecord *obj_new(void) |
| 209 |
{ |
| 210 |
ObjRecord *orp; |
| 211 |
|
| 212 |
orp = obj_clear(nasm_malloc(sizeof(ObjRecord))); |
| 213 |
orp->ori = ori_null; |
| 214 |
return orp; |
| 215 |
} |
| 216 |
|
| 217 |
/* |
| 218 |
* Advance to the next record because the existing one is full or its x_size |
| 219 |
* is incompatible. |
| 220 |
* Any uncommitted data is moved into the next record. |
| 221 |
*/ |
| 222 |
static ObjRecord *obj_bump(ObjRecord * orp) |
| 223 |
{ |
| 224 |
ObjRecord *nxt; |
| 225 |
int used = orp->used; |
| 226 |
int committed = orp->committed; |
| 227 |
|
| 228 |
if (orp->up) { |
| 229 |
*orp->up = nxt = obj_new(); |
| 230 |
nxt->ori = orp->ori; |
| 231 |
nxt->type = orp->type; |
| 232 |
nxt->up = orp->up; |
| 233 |
nxt->back = orp; |
| 234 |
memcpy(nxt->parm, orp->parm, sizeof(orp->parm)); |
| 235 |
} else |
| 236 |
nxt = obj_emit(orp); |
| 237 |
|
| 238 |
used -= committed; |
| 239 |
if (used) { |
| 240 |
nxt->committed = 1; |
| 241 |
nxt->ori(nxt); |
| 242 |
nxt->committed = nxt->used; |
| 243 |
memcpy(nxt->buf + nxt->committed, orp->buf + committed, used); |
| 244 |
nxt->used = nxt->committed + used; |
| 245 |
} |
| 246 |
|
| 247 |
return nxt; |
| 248 |
} |
| 249 |
|
| 250 |
/* |
| 251 |
* Advance to the next record if necessary to allow the next field to fit. |
| 252 |
*/ |
| 253 |
static ObjRecord *obj_check(ObjRecord * orp, int size) |
| 254 |
{ |
| 255 |
if (orp->used + size > RECORD_MAX) |
| 256 |
orp = obj_bump(orp); |
| 257 |
|
| 258 |
if (!orp->committed) { |
| 259 |
orp->committed = 1; |
| 260 |
orp->ori(orp); |
| 261 |
orp->committed = orp->used; |
| 262 |
} |
| 263 |
|
| 264 |
return orp; |
| 265 |
} |
| 266 |
|
| 267 |
/* |
| 268 |
* All data written so far is committed to the current record (won't be moved to |
| 269 |
* the next record in case of continuation). |
| 270 |
*/ |
| 271 |
static ObjRecord *obj_commit(ObjRecord * orp) |
| 272 |
{ |
| 273 |
orp->committed = orp->used; |
| 274 |
return orp; |
| 275 |
} |
| 276 |
|
| 277 |
/* |
| 278 |
* Write a byte |
| 279 |
*/ |
| 280 |
static ObjRecord *obj_byte(ObjRecord * orp, uint8_t val) |
| 281 |
{ |
| 282 |
orp = obj_check(orp, 1); |
| 283 |
orp->buf[orp->used] = val; |
| 284 |
orp->used++; |
| 285 |
return orp; |
| 286 |
} |
| 287 |
|
| 288 |
/* |
| 289 |
* Write a word |
| 290 |
*/ |
| 291 |
static ObjRecord *obj_word(ObjRecord * orp, unsigned int val) |
| 292 |
{ |
| 293 |
orp = obj_check(orp, 2); |
| 294 |
orp->buf[orp->used] = val; |
| 295 |
orp->buf[orp->used + 1] = val >> 8; |
| 296 |
orp->used += 2; |
| 297 |
return orp; |
| 298 |
} |
| 299 |
|
| 300 |
/* |
| 301 |
* Write a reversed word |
| 302 |
*/ |
| 303 |
static ObjRecord *obj_rword(ObjRecord * orp, unsigned int val) |
| 304 |
{ |
| 305 |
orp = obj_check(orp, 2); |
| 306 |
orp->buf[orp->used] = val >> 8; |
| 307 |
orp->buf[orp->used + 1] = val; |
| 308 |
orp->used += 2; |
| 309 |
return orp; |
| 310 |
} |
| 311 |
|
| 312 |
/* |
| 313 |
* Write a dword |
| 314 |
*/ |
| 315 |
static ObjRecord *obj_dword(ObjRecord * orp, uint32_t val) |
| 316 |
{ |
| 317 |
orp = obj_check(orp, 4); |
| 318 |
orp->buf[orp->used] = val; |
| 319 |
orp->buf[orp->used + 1] = val >> 8; |
| 320 |
orp->buf[orp->used + 2] = val >> 16; |
| 321 |
orp->buf[orp->used + 3] = val >> 24; |
| 322 |
orp->used += 4; |
| 323 |
return orp; |
| 324 |
} |
| 325 |
|
| 326 |
/* |
| 327 |
* All fields of "size x" in one obj record must be the same size (either 16 |
| 328 |
* bits or 32 bits). There is a one bit flag in each record which specifies |
| 329 |
* which. |
| 330 |
* This routine is used to force the current record to have the desired |
| 331 |
* x_size. x_size is normally automatic (using obj_x), so that this |
| 332 |
* routine should be used outside obj_x, only to provide compatibility with |
| 333 |
* linkers that have bugs in their processing of the size bit. |
| 334 |
*/ |
| 335 |
|
| 336 |
static ObjRecord *obj_force(ObjRecord * orp, int x) |
| 337 |
{ |
| 338 |
if (orp->x_size == (x ^ 48)) |
| 339 |
orp = obj_bump(orp); |
| 340 |
orp->x_size = x; |
| 341 |
return orp; |
| 342 |
} |
| 343 |
|
| 344 |
/* |
| 345 |
* This routine writes a field of size x. The caller does not need to worry at |
| 346 |
* all about whether 16-bits or 32-bits are required. |
| 347 |
*/ |
| 348 |
static ObjRecord *obj_x(ObjRecord * orp, uint32_t val) |
| 349 |
{ |
| 350 |
if (orp->type & 1) |
| 351 |
orp->x_size = 32; |
| 352 |
if (val > 0xFFFF) |
| 353 |
orp = obj_force(orp, 32); |
| 354 |
if (orp->x_size == 32) { |
| 355 |
ObjRecord *nxt = obj_dword(orp, val); |
| 356 |
nxt->x_size = 32; /* x_size is cleared when a record overflows */ |
| 357 |
return nxt; |
| 358 |
} |
| 359 |
orp->x_size = 16; |
| 360 |
return obj_word(orp, val); |
| 361 |
} |
| 362 |
|
| 363 |
/* |
| 364 |
* Writes an index |
| 365 |
*/ |
| 366 |
static ObjRecord *obj_index(ObjRecord * orp, unsigned int val) |
| 367 |
{ |
| 368 |
if (val < 128) |
| 369 |
return obj_byte(orp, val); |
| 370 |
return obj_word(orp, (val >> 8) | (val << 8) | 0x80); |
| 371 |
} |
| 372 |
|
| 373 |
/* |
| 374 |
* Writes a variable length value |
| 375 |
*/ |
| 376 |
static ObjRecord *obj_value(ObjRecord * orp, uint32_t val) |
| 377 |
{ |
| 378 |
if (val <= 128) |
| 379 |
return obj_byte(orp, val); |
| 380 |
if (val <= 0xFFFF) { |
| 381 |
orp = obj_byte(orp, 129); |
| 382 |
return obj_word(orp, val); |
| 383 |
} |
| 384 |
if (val <= 0xFFFFFF) |
| 385 |
return obj_dword(orp, (val << 8) + 132); |
| 386 |
orp = obj_byte(orp, 136); |
| 387 |
return obj_dword(orp, val); |
| 388 |
} |
| 389 |
|
| 390 |
/* |
| 391 |
* Writes a counted string |
| 392 |
*/ |
| 393 |
static ObjRecord *obj_name(ObjRecord * orp, const char *name) |
| 394 |
{ |
| 395 |
int len = strlen(name); |
| 396 |
uint8_t *ptr; |
| 397 |
|
| 398 |
if (len > UINT8_MAX) { |
| 399 |
nasm_warn(WARN_OTHER, "truncating object name `%.64s...' to %u bytes", |
| 400 |
name, UINT8_MAX); |
| 401 |
len = UINT8_MAX; |
| 402 |
} |
| 403 |
|
| 404 |
orp = obj_check(orp, len + 1); |
| 405 |
ptr = orp->buf + orp->used; |
| 406 |
*ptr++ = len; |
| 407 |
orp->used += len + 1; |
| 408 |
if (obj_uppercase) |
| 409 |
while (--len >= 0) { |
| 410 |
*ptr++ = toupper(*name); |
| 411 |
name++; |
| 412 |
} else |
| 413 |
memcpy(ptr, name, len); |
| 414 |
return orp; |
| 415 |
} |
| 416 |
|
| 417 |
/* |
| 418 |
* Initializer for an LEDATA record. |
| 419 |
* parm[0] = offset |
| 420 |
* parm[1] = segment index |
| 421 |
* During the use of a LEDATA ObjRecord, parm[0] is constantly updated to |
| 422 |
* represent the offset that would be required if the record were split at the |
| 423 |
* last commit point. |
| 424 |
* parm[2] is a copy of parm[0] as it was when the current record was initted. |
| 425 |
*/ |
| 426 |
static void ori_ledata(ObjRecord * orp) |
| 427 |
{ |
| 428 |
obj_index(orp, orp->parm[1]); |
| 429 |
orp->parm[2] = orp->parm[0]; |
| 430 |
obj_x(orp, orp->parm[0]); |
| 431 |
} |
| 432 |
|
| 433 |
/* |
| 434 |
* Initializer for a PUBDEF record. |
| 435 |
* parm[0] = group index |
| 436 |
* parm[1] = segment index |
| 437 |
* parm[2] = frame (only used when both indexes are zero) |
| 438 |
*/ |
| 439 |
static void ori_pubdef(ObjRecord * orp) |
| 440 |
{ |
| 441 |
obj_index(orp, orp->parm[0]); |
| 442 |
obj_index(orp, orp->parm[1]); |
| 443 |
if (!(orp->parm[0] | orp->parm[1])) |
| 444 |
obj_word(orp, orp->parm[2]); |
| 445 |
} |
| 446 |
|
| 447 |
/* |
| 448 |
* Initializer for a LINNUM record. |
| 449 |
* parm[0] = group index |
| 450 |
* parm[1] = segment index |
| 451 |
*/ |
| 452 |
static void ori_linnum(ObjRecord * orp) |
| 453 |
{ |
| 454 |
obj_index(orp, orp->parm[0]); |
| 455 |
obj_index(orp, orp->parm[1]); |
| 456 |
} |
| 457 |
|
| 458 |
/* |
| 459 |
* Initializer for a local vars record. |
| 460 |
*/ |
| 461 |
static void ori_local(ObjRecord * orp) |
| 462 |
{ |
| 463 |
obj_rword(orp, dSYM); |
| 464 |
} |
| 465 |
|
| 466 |
/* |
| 467 |
* Null initializer for records that continue without any header info |
| 468 |
*/ |
| 469 |
static void ori_null(ObjRecord * orp) |
| 470 |
{ |
| 471 |
(void)orp; /* Do nothing */ |
| 472 |
} |
| 473 |
|
| 474 |
/* |
| 475 |
* This concludes the low level section of outobj.c |
| 476 |
*/ |
| 477 |
|
| 478 |
static char obj_infile[FILENAME_MAX]; |
| 479 |
|
| 480 |
static int32_t first_seg; |
| 481 |
static bool any_segs; |
| 482 |
static int passtwo; |
| 483 |
static int arrindex; |
| 484 |
|
| 485 |
#define GROUP_MAX 256 /* we won't _realistically_ have more |
| 486 |
* than this many segs in a group */ |
| 487 |
#define EXT_BLKSIZ 256 /* block size for externals list */ |
| 488 |
|
| 489 |
struct Segment; /* need to know these structs exist */ |
| 490 |
struct Group; |
| 491 |
|
| 492 |
struct LineNumber { |
| 493 |
struct LineNumber *next; |
| 494 |
struct Segment *segment; |
| 495 |
int32_t offset; |
| 496 |
int32_t lineno; |
| 497 |
}; |
| 498 |
|
| 499 |
static struct FileName { |
| 500 |
struct FileName *next; |
| 501 |
char *name; |
| 502 |
struct LineNumber *lnhead, **lntail; |
| 503 |
int index; |
| 504 |
} *fnhead, **fntail; |
| 505 |
|
| 506 |
static struct Array { |
| 507 |
struct Array *next; |
| 508 |
unsigned size; |
| 509 |
int basetype; |
| 510 |
} *arrhead, **arrtail; |
| 511 |
|
| 512 |
#define ARRAYBOT 31 /* magic number for first array index */ |
| 513 |
|
| 514 |
static struct Public { |
| 515 |
struct Public *next; |
| 516 |
char *name; |
| 517 |
int32_t offset; |
| 518 |
int32_t segment; /* only if it's far-absolute */ |
| 519 |
int type; /* only for local debug syms */ |
| 520 |
} *fpubhead, **fpubtail, *last_defined; |
| 521 |
|
| 522 |
static struct External { |
| 523 |
struct External *next; |
| 524 |
char *name; |
| 525 |
int32_t commonsize; |
| 526 |
int32_t commonelem; /* element size if FAR, else zero */ |
| 527 |
int index; /* OBJ-file external index */ |
| 528 |
enum { |
| 529 |
DEFWRT_NONE, /* no unusual default-WRT */ |
| 530 |
DEFWRT_STRING, /* a string we don't yet understand */ |
| 531 |
DEFWRT_SEGMENT, /* a segment */ |
| 532 |
DEFWRT_GROUP /* a group */ |
| 533 |
} defwrt_type; |
| 534 |
union { |
| 535 |
char *string; |
| 536 |
struct Segment *seg; |
| 537 |
struct Group *grp; |
| 538 |
} defwrt_ptr; |
| 539 |
struct External *next_dws; /* next with DEFWRT_STRING */ |
| 540 |
} *exthead, **exttail, *dws; |
| 541 |
|
| 542 |
static int externals; |
| 543 |
|
| 544 |
static struct ExtBack { |
| 545 |
struct ExtBack *next; |
| 546 |
struct External *exts[EXT_BLKSIZ]; |
| 547 |
} *ebhead, **ebtail; |
| 548 |
|
| 549 |
static struct Segment { |
| 550 |
struct Segment *next; |
| 551 |
char *name; |
| 552 |
int32_t index; /* the NASM segment id */ |
| 553 |
int32_t obj_index; /* the OBJ-file segment index */ |
| 554 |
struct Group *grp; /* the group it beint32_ts to */ |
| 555 |
uint32_t currentpos; |
| 556 |
int32_t align; /* can be SEG_ABS + absolute addr */ |
| 557 |
uint64_t origalign; /* originally requested alignment */ |
| 558 |
int64_t pass_last_seen; |
| 559 |
struct Public *pubhead, **pubtail, *lochead, **loctail; |
| 560 |
char *segclass, *overlay; /* `class' is a C++ keyword :-) */ |
| 561 |
ObjRecord *orp; |
| 562 |
enum { |
| 563 |
CMB_PRIVATE = 0, |
| 564 |
CMB_PUBLIC = 2, |
| 565 |
CMB_STACK = 5, |
| 566 |
CMB_COMMON = 6 |
| 567 |
} combine; |
| 568 |
bool use32; /* is this segment 32-bit? */ |
| 569 |
} *seghead, **segtail, *obj_seg_needs_update; |
| 570 |
|
| 571 |
static struct Group { |
| 572 |
struct Group *next; |
| 573 |
char *name; |
| 574 |
int32_t index; /* NASM segment id */ |
| 575 |
int32_t obj_index; /* OBJ-file group index */ |
| 576 |
int32_t nentries; /* number of elements... */ |
| 577 |
int32_t nindices; /* ...and number of index elts... */ |
| 578 |
union { |
| 579 |
int32_t index; |
| 580 |
char *name; |
| 581 |
} segs[GROUP_MAX]; /* ...in this */ |
| 582 |
} *grphead, **grptail, *obj_grp_needs_update; |
| 583 |
|
| 584 |
static struct ImpDef { |
| 585 |
struct ImpDef *next; |
| 586 |
char *extname; |
| 587 |
char *libname; |
| 588 |
unsigned int impindex; |
| 589 |
char *impname; |
| 590 |
} *imphead, **imptail; |
| 591 |
|
| 592 |
static struct ExpDef { |
| 593 |
struct ExpDef *next; |
| 594 |
char *intname; |
| 595 |
char *extname; |
| 596 |
unsigned int ordinal; |
| 597 |
int flags; |
| 598 |
} *exphead, **exptail; |
| 599 |
|
| 600 |
#define EXPDEF_FLAG_ORDINAL 0x80 |
| 601 |
#define EXPDEF_FLAG_RESIDENT 0x40 |
| 602 |
#define EXPDEF_FLAG_NODATA 0x20 |
| 603 |
#define EXPDEF_MASK_PARMCNT 0x1F |
| 604 |
|
| 605 |
struct SegmentToClass { |
| 606 |
const char *segment; /* segment */ |
| 607 |
const char *segclass; /* class */ |
| 608 |
}; |
| 609 |
|
| 610 |
static int32_t obj_entry_seg, obj_entry_ofs; |
| 611 |
|
| 612 |
const struct ofmt of_obj; |
| 613 |
static const struct dfmt borland_debug_form; |
| 614 |
|
| 615 |
/* The current segment */ |
| 616 |
static struct Segment *current_seg; |
| 617 |
|
| 618 |
/* Name for segment to use if no segment directive is defined */ |
| 619 |
static char DEFAULT_SEG[] = "__NASMDEFSEG"; |
| 620 |
|
| 621 |
/* Conversion table from known segments to default classes */ |
| 622 |
static const struct SegmentToClass conv_table[] = { |
| 623 |
/* known segments, default class */ |
| 624 |
{ "CODE", "CODE" }, |
| 625 |
{ "TEXT", "CODE" }, |
| 626 |
{ "CONST", "CONST" }, |
| 627 |
{ "DATA", "DATA" }, |
| 628 |
{ "BSS", "BSS" }, |
| 629 |
{ "STACK", "STACK" }, |
| 630 |
{ "CODE32", "CODE" }, |
| 631 |
{ "TEXT32", "CODE" }, |
| 632 |
{ "CONST32", "CONST" }, |
| 633 |
{ "DATA32", "DATA" }, |
| 634 |
{ "BSS32", "BSS" }, |
| 635 |
{ "STACK32", "STACK" }, |
| 636 |
{ NULL, NULL }, |
| 637 |
}; |
| 638 |
|
| 639 |
static int32_t obj_segment(char *, int *); |
| 640 |
static void obj_write_file(void); |
| 641 |
static enum directive_result obj_directive(enum directive, char *); |
| 642 |
|
| 643 |
static const char *get_default_class(const char *segment) |
| 644 |
{ |
| 645 |
const struct SegmentToClass *conv; |
| 646 |
|
| 647 |
if (segment && segment[0]) { |
| 648 |
for (conv = conv_table; conv->segment; conv++) { |
| 649 |
if (!strcmp(segment, conv->segment)) |
| 650 |
return conv->segclass; |
| 651 |
} |
| 652 |
} |
| 653 |
|
| 654 |
return NULL; |
| 655 |
} |
| 656 |
|
| 657 |
static void obj_init(void) |
| 658 |
{ |
| 659 |
strlcpy(obj_infile, inname, sizeof(obj_infile)); |
| 660 |
first_seg = seg_alloc(); |
| 661 |
any_segs = false; |
| 662 |
fpubhead = NULL; |
| 663 |
fpubtail = &fpubhead; |
| 664 |
exthead = NULL; |
| 665 |
exttail = &exthead; |
| 666 |
imphead = NULL; |
| 667 |
imptail = &imphead; |
| 668 |
exphead = NULL; |
| 669 |
exptail = &exphead; |
| 670 |
dws = NULL; |
| 671 |
externals = 0; |
| 672 |
ebhead = NULL; |
| 673 |
ebtail = &ebhead; |
| 674 |
seghead = obj_seg_needs_update = NULL; |
| 675 |
segtail = &seghead; |
| 676 |
grphead = obj_grp_needs_update = NULL; |
| 677 |
grptail = &grphead; |
| 678 |
obj_entry_seg = NO_SEG; |
| 679 |
obj_uppercase = false; |
| 680 |
obj_use32 = false; |
| 681 |
passtwo = 0; |
| 682 |
current_seg = NULL; |
| 683 |
|
| 684 |
/* |
| 685 |
* Convert known Unix sections to OMF segments via macros. |
| 686 |
*/ |
| 687 |
if (ofmt == &of_obj2) { |
| 688 |
char section_text[] = ".text=TEXT32"; |
| 689 |
char section_rodata[] = ".rodata=CONST32"; |
| 690 |
char section_data[] = ".data=DATA32"; |
| 691 |
char section_bss[] = ".bss=BSS32"; |
| 692 |
|
| 693 |
pp_pre_define(section_text); |
| 694 |
pp_pre_define(section_rodata); |
| 695 |
pp_pre_define(section_data); |
| 696 |
pp_pre_define(section_bss); |
| 697 |
} |
| 698 |
} |
| 699 |
|
| 700 |
static void obj_cleanup(void) |
| 701 |
{ |
| 702 |
obj_write_file(); |
| 703 |
dfmt->cleanup(); |
| 704 |
while (seghead) { |
| 705 |
struct Segment *segtmp = seghead; |
| 706 |
seghead = seghead->next; |
| 707 |
while (segtmp->pubhead) { |
| 708 |
struct Public *pubtmp = segtmp->pubhead; |
| 709 |
segtmp->pubhead = pubtmp->next; |
| 710 |
nasm_free(pubtmp->name); |
| 711 |
nasm_free(pubtmp); |
| 712 |
} |
| 713 |
nasm_free(segtmp->segclass); |
| 714 |
nasm_free(segtmp->overlay); |
| 715 |
nasm_free(segtmp); |
| 716 |
} |
| 717 |
while (fpubhead) { |
| 718 |
struct Public *pubtmp = fpubhead; |
| 719 |
fpubhead = fpubhead->next; |
| 720 |
nasm_free(pubtmp->name); |
| 721 |
nasm_free(pubtmp); |
| 722 |
} |
| 723 |
while (exthead) { |
| 724 |
struct External *exttmp = exthead; |
| 725 |
exthead = exthead->next; |
| 726 |
nasm_free(exttmp); |
| 727 |
} |
| 728 |
while (imphead) { |
| 729 |
struct ImpDef *imptmp = imphead; |
| 730 |
imphead = imphead->next; |
| 731 |
nasm_free(imptmp->extname); |
| 732 |
nasm_free(imptmp->libname); |
| 733 |
nasm_free(imptmp->impname); /* nasm_free won't mind if it's NULL */ |
| 734 |
nasm_free(imptmp); |
| 735 |
} |
| 736 |
while (exphead) { |
| 737 |
struct ExpDef *exptmp = exphead; |
| 738 |
exphead = exphead->next; |
| 739 |
nasm_free(exptmp->extname); |
| 740 |
nasm_free(exptmp->intname); |
| 741 |
nasm_free(exptmp); |
| 742 |
} |
| 743 |
while (ebhead) { |
| 744 |
struct ExtBack *ebtmp = ebhead; |
| 745 |
ebhead = ebhead->next; |
| 746 |
nasm_free(ebtmp); |
| 747 |
} |
| 748 |
while (grphead) { |
| 749 |
struct Group *grptmp = grphead; |
| 750 |
grphead = grphead->next; |
| 751 |
nasm_free(grptmp); |
| 752 |
} |
| 753 |
} |
| 754 |
|
| 755 |
static void obj_ext_set_defwrt(struct External *ext, char *id) |
| 756 |
{ |
| 757 |
struct Segment *seg; |
| 758 |
struct Group *grp; |
| 759 |
|
| 760 |
for (seg = seghead; seg; seg = seg->next) |
| 761 |
if (!strcmp(seg->name, id)) { |
| 762 |
ext->defwrt_type = DEFWRT_SEGMENT; |
| 763 |
ext->defwrt_ptr.seg = seg; |
| 764 |
nasm_free(id); |
| 765 |
return; |
| 766 |
} |
| 767 |
|
| 768 |
for (grp = grphead; grp; grp = grp->next) |
| 769 |
if (!strcmp(grp->name, id)) { |
| 770 |
ext->defwrt_type = DEFWRT_GROUP; |
| 771 |
ext->defwrt_ptr.grp = grp; |
| 772 |
nasm_free(id); |
| 773 |
return; |
| 774 |
} |
| 775 |
|
| 776 |
ext->defwrt_type = DEFWRT_STRING; |
| 777 |
ext->defwrt_ptr.string = id; |
| 778 |
ext->next_dws = dws; |
| 779 |
dws = ext; |
| 780 |
} |
| 781 |
|
| 782 |
static void obj_deflabel(char *name, int32_t segment, |
| 783 |
int64_t offset, int is_global, char *special) |
| 784 |
{ |
| 785 |
/* |
| 786 |
* We have three cases: |
| 787 |
* |
| 788 |
* (i) `segment' is a segment-base. If so, set the name field |
| 789 |
* for the segment or group structure it refers to, and then |
| 790 |
* return. |
| 791 |
* |
| 792 |
* (ii) `segment' is one of our segments, or a SEG_ABS segment. |
| 793 |
* Save the label position for later output of a PUBDEF record. |
| 794 |
* (Or a MODPUB, if we work out how.) |
| 795 |
* |
| 796 |
* (iii) `segment' is not one of our segments. Save the label |
| 797 |
* position for later output of an EXTDEF, and also store a |
| 798 |
* back-reference so that we can map later references to this |
| 799 |
* segment number to the external index. |
| 800 |
*/ |
| 801 |
struct External *ext; |
| 802 |
struct ExtBack *eb; |
| 803 |
struct Segment *seg; |
| 804 |
int i; |
| 805 |
bool used_special = false; /* have we used the special text? */ |
| 806 |
|
| 807 |
nasm_debug(2, " obj_deflabel: %s, seg=%"PRIx32", off=%"PRIx64", is_global=%d, %s\n", |
| 808 |
name, segment, offset, is_global, special); |
| 809 |
|
| 810 |
/* |
| 811 |
* If it's a special-retry from pass two, discard it. |
| 812 |
*/ |
| 813 |
if (is_global == 3) |
| 814 |
return; |
| 815 |
|
| 816 |
/* |
| 817 |
* First check for the double-period, signifying something |
| 818 |
* unusual. |
| 819 |
*/ |
| 820 |
if (name[0] == '.' && name[1] == '.' && name[2] != '@') { |
| 821 |
if (!strcmp(name, "..start")) { |
| 822 |
obj_entry_seg = segment; |
| 823 |
obj_entry_ofs = offset; |
| 824 |
return; |
| 825 |
} |
| 826 |
nasm_nonfatal("unrecognised special symbol `%s'", name); |
| 827 |
} |
| 828 |
|
| 829 |
/* |
| 830 |
* Case (i): |
| 831 |
*/ |
| 832 |
if (obj_seg_needs_update) { |
| 833 |
obj_seg_needs_update->name = name; |
| 834 |
return; |
| 835 |
} else if (obj_grp_needs_update) { |
| 836 |
obj_grp_needs_update->name = name; |
| 837 |
return; |
| 838 |
} |
| 839 |
if (segment < SEG_ABS && segment != NO_SEG && segment % 2) |
| 840 |
return; |
| 841 |
|
| 842 |
if (segment >= SEG_ABS || segment == NO_SEG) { |
| 843 |
/* |
| 844 |
* SEG_ABS subcase of (ii). |
| 845 |
*/ |
| 846 |
if (is_global) { |
| 847 |
struct Public *pub; |
| 848 |
|
| 849 |
pub = *fpubtail = nasm_malloc(sizeof(*pub)); |
| 850 |
fpubtail = &pub->next; |
| 851 |
pub->next = NULL; |
| 852 |
pub->name = nasm_strdup(name); |
| 853 |
pub->offset = offset; |
| 854 |
pub->segment = (segment == NO_SEG ? 0 : segment & ~SEG_ABS); |
| 855 |
} |
| 856 |
if (special) |
| 857 |
nasm_nonfatal("OBJ supports no special symbol features" |
| 858 |
" for this symbol type"); |
| 859 |
return; |
| 860 |
} |
| 861 |
|
| 862 |
/* |
| 863 |
* If `any_segs' is still false, we might need to define a |
| 864 |
* default segment, if they're trying to declare a label in |
| 865 |
* `first_seg'. |
| 866 |
*/ |
| 867 |
if (!any_segs && segment == first_seg) { |
| 868 |
int tempint = 0; |
| 869 |
if (segment != obj_segment(DEFAULT_SEG, &tempint)) |
| 870 |
nasm_panic("strange segment conditions in OBJ driver"); |
| 871 |
} |
| 872 |
|
| 873 |
for (seg = seghead; seg && is_global; seg = seg->next) |
| 874 |
if (seg->index == segment) { |
| 875 |
struct Public *loc = nasm_malloc(sizeof(*loc)); |
| 876 |
/* |
| 877 |
* Case (ii). Maybe MODPUB someday? |
| 878 |
*/ |
| 879 |
*seg->pubtail = loc; |
| 880 |
seg->pubtail = &loc->next; |
| 881 |
loc->next = NULL; |
| 882 |
loc->name = nasm_strdup(name); |
| 883 |
loc->offset = offset; |
| 884 |
|
| 885 |
if (special) |
| 886 |
nasm_nonfatal("OBJ supports no special symbol features" |
| 887 |
" for this symbol type"); |
| 888 |
return; |
| 889 |
} |
| 890 |
|
| 891 |
/* |
| 892 |
* Case (iii). |
| 893 |
*/ |
| 894 |
if (is_global) { |
| 895 |
ext = *exttail = nasm_malloc(sizeof(*ext)); |
| 896 |
ext->next = NULL; |
| 897 |
exttail = &ext->next; |
| 898 |
ext->name = name; |
| 899 |
/* Place by default all externs into the current segment */ |
| 900 |
ext->defwrt_type = DEFWRT_NONE; |
| 901 |
|
| 902 |
/* 28-Apr-2002 - John Coffman |
| 903 |
The following code was introduced on 12-Aug-2000, and breaks fixups |
| 904 |
on code passed thru the MSC 5.1 linker (3.66) and MSC 6.00A linker |
| 905 |
(5.10). It was introduced after FIXUP32 was added, and may be needed |
| 906 |
for 32-bit segments. The following will get 16-bit segments working |
| 907 |
again, and maybe someone can correct the 'if' condition which is |
| 908 |
actually needed. |
| 909 |
*/ |
| 910 |
#if 0 |
| 911 |
if (current_seg) { |
| 912 |
#else |
| 913 |
if (current_seg && current_seg->use32) { |
| 914 |
if (current_seg->grp) { |
| 915 |
ext->defwrt_type = DEFWRT_GROUP; |
| 916 |
ext->defwrt_ptr.grp = current_seg->grp; |
| 917 |
} else { |
| 918 |
ext->defwrt_type = DEFWRT_SEGMENT; |
| 919 |
ext->defwrt_ptr.seg = current_seg; |
| 920 |
} |
| 921 |
} |
| 922 |
#endif |
| 923 |
|
| 924 |
if (is_global == 2) { |
| 925 |
ext->commonsize = offset; |
| 926 |
ext->commonelem = 1; /* default FAR */ |
| 927 |
} else |
| 928 |
ext->commonsize = 0; |
| 929 |
} else |
| 930 |
return; |
| 931 |
|
| 932 |
/* |
| 933 |
* Now process the special text, if any, to find default-WRT |
| 934 |
* specifications and common-variable element-size and near/far |
| 935 |
* specifications. |
| 936 |
*/ |
| 937 |
while (special && *special) { |
| 938 |
used_special = true; |
| 939 |
|
| 940 |
/* |
| 941 |
* We might have a default-WRT specification. |
| 942 |
*/ |
| 943 |
if (!nasm_strnicmp(special, "wrt", 3)) { |
| 944 |
char *p; |
| 945 |
int len; |
| 946 |
special += 3; |
| 947 |
special += strspn(special, " \t"); |
| 948 |
p = nasm_strndup(special, len = strcspn(special, ":")); |
| 949 |
obj_ext_set_defwrt(ext, p); |
| 950 |
special += len; |
| 951 |
if (*special && *special != ':') |
| 952 |
nasm_nonfatal("`:' expected in special symbol" |
| 953 |
" text for `%s'", ext->name); |
| 954 |
else if (*special == ':') |
| 955 |
special++; |
| 956 |
} |
| 957 |
|
| 958 |
/* |
| 959 |
* The NEAR or FAR keywords specify nearness or |
| 960 |
* farness. FAR gives default element size 1. |
| 961 |
*/ |
| 962 |
if (!nasm_strnicmp(special, "far", 3)) { |
| 963 |
if (ext->commonsize) |
| 964 |
ext->commonelem = 1; |
| 965 |
else |
| 966 |
nasm_nonfatal("`%s': `far' keyword may only be applied" |
| 967 |
" to common variables\n", ext->name); |
| 968 |
special += 3; |
| 969 |
special += strspn(special, " \t"); |
| 970 |
} else if (!nasm_strnicmp(special, "near", 4)) { |
| 971 |
if (ext->commonsize) |
| 972 |
ext->commonelem = 0; |
| 973 |
else |
| 974 |
nasm_nonfatal("`%s': `far' keyword may only be applied" |
| 975 |
" to common variables\n", ext->name); |
| 976 |
special += 4; |
| 977 |
special += strspn(special, " \t"); |
| 978 |
} |
| 979 |
|
| 980 |
/* |
| 981 |
* If it's a common, and anything else remains on the line |
| 982 |
* before a further colon, evaluate it as an expression and |
| 983 |
* use that as the element size. Forward references aren't |
| 984 |
* allowed. |
| 985 |
*/ |
| 986 |
if (*special == ':') |
| 987 |
special++; |
| 988 |
else if (*special) { |
| 989 |
if (ext->commonsize) { |
| 990 |
expr *e; |
| 991 |
struct tokenval tokval; |
| 992 |
|
| 993 |
stdscan_reset(special); |
| 994 |
tokval.t_type = TOKEN_INVALID; |
| 995 |
e = evaluate(stdscan, NULL, &tokval, NULL, 1, NULL); |
| 996 |
if (e) { |
| 997 |
if (!is_simple(e)) |
| 998 |
nasm_nonfatal("cannot use relocatable" |
| 999 |
" expression as common-variable element size"); |
| 1000 |
else |
| 1001 |
ext->commonelem = reloc_value(e); |
| 1002 |
} |
| 1003 |
special = stdscan_tell(); |
| 1004 |
} else { |
| 1005 |
nasm_nonfatal("`%s': element-size specifications only" |
| 1006 |
" apply to common variables", ext->name); |
| 1007 |
while (*special && *special != ':') |
| 1008 |
special++; |
| 1009 |
if (*special == ':') |
| 1010 |
special++; |
| 1011 |
} |
| 1012 |
} |
| 1013 |
} |
| 1014 |
|
| 1015 |
i = segment / 2; |
| 1016 |
eb = ebhead; |
| 1017 |
if (!eb) { |
| 1018 |
eb = *ebtail = nasm_zalloc(sizeof(*eb)); |
| 1019 |
eb->next = NULL; |
| 1020 |
ebtail = &eb->next; |
| 1021 |
} |
| 1022 |
while (i >= EXT_BLKSIZ) { |
| 1023 |
if (eb && eb->next) |
| 1024 |
eb = eb->next; |
| 1025 |
else { |
| 1026 |
eb = *ebtail = nasm_zalloc(sizeof(*eb)); |
| 1027 |
eb->next = NULL; |
| 1028 |
ebtail = &eb->next; |
| 1029 |
} |
| 1030 |
i -= EXT_BLKSIZ; |
| 1031 |
} |
| 1032 |
eb->exts[i] = ext; |
| 1033 |
ext->index = ++externals; |
| 1034 |
|
| 1035 |
if (special && !used_special) |
| 1036 |
nasm_nonfatal("OBJ supports no special symbol features" |
| 1037 |
" for this symbol type"); |
| 1038 |
} |
| 1039 |
|
| 1040 |
/* forward declaration */ |
| 1041 |
static void obj_write_fixup(ObjRecord * orp, int bytes, |
| 1042 |
int segrel, int32_t seg, int32_t wrt, |
| 1043 |
struct Segment *segto); |
| 1044 |
|
| 1045 |
static void obj_out(const struct out_data *out) |
| 1046 |
{ |
| 1047 |
OUT_LEGACY(out,segto,data,type,size,segment,wrt); |
| 1048 |
const uint8_t *ucdata; |
| 1049 |
int32_t ldata; |
| 1050 |
struct Segment *seg; |
| 1051 |
ObjRecord *orp; |
| 1052 |
|
| 1053 |
/* |
| 1054 |
* If `any_segs' is still false, we must define a default |
| 1055 |
* segment. |
| 1056 |
*/ |
| 1057 |
if (!any_segs) { |
| 1058 |
int tempint = 0; |
| 1059 |
if (segto != obj_segment(DEFAULT_SEG, &tempint)) |
| 1060 |
nasm_panic("strange segment conditions in OBJ driver"); |
| 1061 |
} |
| 1062 |
|
| 1063 |
/* |
| 1064 |
* Find the segment we are targeting. |
| 1065 |
*/ |
| 1066 |
for (seg = seghead; seg; seg = seg->next) |
| 1067 |
if (seg->index == segto) |
| 1068 |
break; |
| 1069 |
if (!seg) |
| 1070 |
nasm_panic("code directed to nonexistent segment?"); |
| 1071 |
|
| 1072 |
orp = seg->orp; |
| 1073 |
orp->parm[0] = seg->currentpos; |
| 1074 |
|
| 1075 |
switch (type) { |
| 1076 |
case OUT_RAWDATA: |
| 1077 |
ucdata = data; |
| 1078 |
while (size > 0) { |
| 1079 |
unsigned int len; |
| 1080 |
orp = obj_check(seg->orp, 1); |
| 1081 |
len = RECORD_MAX - orp->used; |
| 1082 |
if (len > size) |
| 1083 |
len = size; |
| 1084 |
memcpy(orp->buf + orp->used, ucdata, len); |
| 1085 |
orp->committed = orp->used += len; |
| 1086 |
orp->parm[0] = seg->currentpos += len; |
| 1087 |
ucdata += len; |
| 1088 |
size -= len; |
| 1089 |
} |
| 1090 |
break; |
| 1091 |
|
| 1092 |
case OUT_ADDRESS: |
| 1093 |
case OUT_REL1ADR: |
| 1094 |
case OUT_REL2ADR: |
| 1095 |
case OUT_REL4ADR: |
| 1096 |
case OUT_REL8ADR: |
| 1097 |
{ |
| 1098 |
int rsize; |
| 1099 |
|
| 1100 |
if (type == OUT_ADDRESS) |
| 1101 |
size = abs((int)size); |
| 1102 |
|
| 1103 |
if (segment == NO_SEG && type != OUT_ADDRESS) |
| 1104 |
nasm_nonfatal("relative call to absolute address not" |
| 1105 |
" supported by OBJ format"); |
| 1106 |
if (segment >= SEG_ABS) |
| 1107 |
nasm_nonfatal("far-absolute relocations not supported" |
| 1108 |
" by OBJ format"); |
| 1109 |
|
| 1110 |
ldata = *(int64_t *)data; |
| 1111 |
if (type != OUT_ADDRESS) { |
| 1112 |
/* |
| 1113 |
* For 16-bit and 32-bit x86 code, the size and realsize() always |
| 1114 |
* matches as only jumps, calls and loops uses PC relative |
| 1115 |
* addressing and the address isn't followed by any other opcode |
| 1116 |
* bytes. In 64-bit mode there is RIP relative addressing which |
| 1117 |
* means the fixup location can be followed by an immediate value, |
| 1118 |
* meaning that size > realsize(). |
| 1119 |
* |
| 1120 |
* When the CPU is calculating the effective address, it takes the |
| 1121 |
* RIP at the end of the instruction and adds the fixed up relative |
| 1122 |
* address value to it. |
| 1123 |
* |
| 1124 |
* The linker's point of reference is the end of the fixup location |
| 1125 |
* (which is the end of the instruction for Jcc, CALL, LOOP[cc]). |
| 1126 |
* It is calculating distance between the target symbol and the end |
| 1127 |
* of the fixup location, and add this to the displacement value we |
| 1128 |
* are calculating here and storing at the fixup location. |
| 1129 |
* |
| 1130 |
* To get the right effect, we need to _reduce_ the displacement |
| 1131 |
* value by the number of bytes following the fixup. |
| 1132 |
* |
| 1133 |
* Example: |
| 1134 |
* data at address 0x100; REL4ADR at 0x050, 4 byte immediate, |
| 1135 |
* end of fixup at 0x054, end of instruction at 0x058. |
| 1136 |
* => size = 8. |
| 1137 |
* => realsize() -> 4 |
| 1138 |
* => CPU needs a value of: 0x100 - 0x058 = 0x0a8 |
| 1139 |
* => linker/loader will add: 0x100 - 0x054 = 0x0ac |
| 1140 |
* => We must add an addend of -4. |
| 1141 |
* => realsize() - size = -4. |
| 1142 |
* |
| 1143 |
* The code used to do size - realsize() at least since v0.90, |
| 1144 |
* probably because it wasn't needed... |
| 1145 |
*/ |
| 1146 |
ldata -= size; |
| 1147 |
size = realsize(type, size); |
| 1148 |
ldata += size; |
| 1149 |
} |
| 1150 |
|
| 1151 |
switch (size) { |
| 1152 |
default: |
| 1153 |
nasm_nonfatal("OBJ format can only handle 16- or " |
| 1154 |
"32-byte relocations"); |
| 1155 |
segment = NO_SEG; /* Don't actually generate a relocation */ |
| 1156 |
break; |
| 1157 |
case 2: |
| 1158 |
orp = obj_word(orp, ldata); |
| 1159 |
break; |
| 1160 |
case 4: |
| 1161 |
orp = obj_dword(orp, ldata); |
| 1162 |
break; |
| 1163 |
} |
| 1164 |
|
| 1165 |
rsize = size; |
| 1166 |
if (segment < SEG_ABS && (segment != NO_SEG && segment % 2) && |
| 1167 |
size == 4) { |
| 1168 |
/* |
| 1169 |
* This is a 4-byte segment-base relocation such as |
| 1170 |
* `MOV EAX,SEG foo'. OBJ format can't actually handle |
| 1171 |
* these, but if the constant term has the 16 low bits |
| 1172 |
* zero, we can just apply a 2-byte segment-base |
| 1173 |
* relocation to the low word instead. |
| 1174 |
*/ |
| 1175 |
rsize = 2; |
| 1176 |
if (ldata & 0xFFFF) |
| 1177 |
nasm_nonfatal("OBJ format cannot handle complex" |
| 1178 |
" dword-size segment base references"); |
| 1179 |
} |
| 1180 |
if (segment != NO_SEG) |
| 1181 |
obj_write_fixup(orp, rsize, |
| 1182 |
(type == OUT_ADDRESS ? 0x4000 : 0), |
| 1183 |
segment, wrt, seg); |
| 1184 |
seg->currentpos += size; |
| 1185 |
break; |
| 1186 |
} |
| 1187 |
|
| 1188 |
default: |
| 1189 |
nasm_nonfatal("Relocation type not supported by output format"); |
| 1190 |
/* fall through */ |
| 1191 |
|
| 1192 |
case OUT_RESERVE: |
| 1193 |
if (orp->committed) |
| 1194 |
orp = obj_bump(orp); |
| 1195 |
seg->currentpos += size; |
| 1196 |
break; |
| 1197 |
} |
| 1198 |
obj_commit(orp); |
| 1199 |
} |
| 1200 |
|
| 1201 |
static void obj_write_fixup(ObjRecord * orp, int bytes, |
| 1202 |
int segrel, int32_t seg, int32_t wrt, |
| 1203 |
struct Segment *segto) |
| 1204 |
{ |
| 1205 |
unsigned locat; |
| 1206 |
int method; |
| 1207 |
int base; |
| 1208 |
int32_t tidx, fidx; |
| 1209 |
struct Segment *s = NULL; |
| 1210 |
struct Group *g = NULL; |
| 1211 |
struct External *e = NULL; |
| 1212 |
ObjRecord *forp; |
| 1213 |
|
| 1214 |
if (bytes != 2 && bytes != 4) { |
| 1215 |
nasm_nonfatal("`obj' output driver does not support" |
| 1216 |
" %d-bit relocations", bytes << 3); |
| 1217 |
return; |
| 1218 |
} |
| 1219 |
|
| 1220 |
forp = orp->child; |
| 1221 |
if (forp == NULL) { |
| 1222 |
orp->child = forp = obj_new(); |
| 1223 |
forp->up = &(orp->child); |
| 1224 |
/* We should choose between FIXUPP and FIXU32 record type */ |
| 1225 |
/* If we're targeting a 32-bit segment, use a FIXU32 record */ |
| 1226 |
if (segto->use32) |
| 1227 |
forp->type = FIXU32; |
| 1228 |
else |
| 1229 |
forp->type = FIXUPP; |
| 1230 |
} |
| 1231 |
|
| 1232 |
if (seg % 2) { |
| 1233 |
base = true; |
| 1234 |
locat = FIX_16_SELECTOR; |
| 1235 |
seg--; |
| 1236 |
if (bytes != 2) |
| 1237 |
nasm_panic("OBJ: 4-byte segment base fixup got" |
| 1238 |
" through sanity check"); |
| 1239 |
} else { |
| 1240 |
base = false; |
| 1241 |
locat = (bytes == 2) ? FIX_16_OFFSET : FIX_32_OFFSET; |
| 1242 |
if (!segrel) |
| 1243 |
/* |
| 1244 |
* There is a bug in tlink that makes it process self relative |
| 1245 |
* fixups incorrectly if the x_size doesn't match the location |
| 1246 |
* size. |
| 1247 |
*/ |
| 1248 |
forp = obj_force(forp, bytes << 3); |
| 1249 |
} |
| 1250 |
|
| 1251 |
forp = obj_rword(forp, locat | segrel | (orp->parm[0] - orp->parm[2])); |
| 1252 |
|
| 1253 |
tidx = fidx = -1, method = 0; /* placate optimisers */ |
| 1254 |
|
| 1255 |
/* |
| 1256 |
* See if we can find the segment ID in our segment list. If |
| 1257 |
* so, we have a T4 (LSEG) target. |
| 1258 |
*/ |
| 1259 |
for (s = seghead; s; s = s->next) |
| 1260 |
if (s->index == seg) |
| 1261 |
break; |
| 1262 |
if (s) |
| 1263 |
method = 4, tidx = s->obj_index; |
| 1264 |
else { |
| 1265 |
for (g = grphead; g; g = g->next) |
| 1266 |
if (g->index == seg) |
| 1267 |
break; |
| 1268 |
if (g) |
| 1269 |
method = 5, tidx = g->obj_index; |
| 1270 |
else { |
| 1271 |
int32_t i = seg / 2; |
| 1272 |
struct ExtBack *eb = ebhead; |
| 1273 |
while (i >= EXT_BLKSIZ) { |
| 1274 |
if (eb) |
| 1275 |
eb = eb->next; |
| 1276 |
else |
| 1277 |
break; |
| 1278 |
i -= EXT_BLKSIZ; |
| 1279 |
} |
| 1280 |
if (eb) |
| 1281 |
method = 6, e = eb->exts[i], tidx = e->index; |
| 1282 |
else |
| 1283 |
nasm_panic("unrecognised segment value in obj_write_fixup"); |
| 1284 |
} |
| 1285 |
} |
| 1286 |
|
| 1287 |
/* |
| 1288 |
* If no WRT given, assume the natural default, which is method |
| 1289 |
* F5 unless: |
| 1290 |
* |
| 1291 |
* - we are doing an OFFSET fixup for a grouped segment, in |
| 1292 |
* which case we require F1 (group). |
| 1293 |
* |
| 1294 |
* - we are doing an OFFSET fixup for an external with a |
| 1295 |
* default WRT, in which case we must honour the default WRT. |
| 1296 |
*/ |
| 1297 |
if (wrt == NO_SEG) { |
| 1298 |
if (!base && s && s->grp) |
| 1299 |
method |= 0x10, fidx = s->grp->obj_index; |
| 1300 |
else if (!base && e && e->defwrt_type != DEFWRT_NONE) { |
| 1301 |
if (e->defwrt_type == DEFWRT_SEGMENT) |
| 1302 |
method |= 0x00, fidx = e->defwrt_ptr.seg->obj_index; |
| 1303 |
else if (e->defwrt_type == DEFWRT_GROUP) |
| 1304 |
method |= 0x10, fidx = e->defwrt_ptr.grp->obj_index; |
| 1305 |
else { |
| 1306 |
nasm_nonfatal("default WRT specification for" |
| 1307 |
" external `%s' unresolved", e->name); |
| 1308 |
method |= 0x50, fidx = -1; /* got to do _something_ */ |
| 1309 |
} |
| 1310 |
} else |
| 1311 |
method |= 0x50, fidx = -1; |
| 1312 |
} else { |
| 1313 |
/* |
| 1314 |
* See if we can find the WRT-segment ID in our segment |
| 1315 |
* list. If so, we have a F0 (LSEG) frame. |
| 1316 |
*/ |
| 1317 |
for (s = seghead; s; s = s->next) |
| 1318 |
if (s->index == wrt - 1) |
| 1319 |
break; |
| 1320 |
if (s) |
| 1321 |
method |= 0x00, fidx = s->obj_index; |
| 1322 |
else { |
| 1323 |
for (g = grphead; g; g = g->next) |
| 1324 |
if (g->index == wrt - 1) |
| 1325 |
break; |
| 1326 |
if (g) |
| 1327 |
method |= 0x10, fidx = g->obj_index; |
| 1328 |
else { |
| 1329 |
int32_t i = wrt / 2; |
| 1330 |
struct ExtBack *eb = ebhead; |
| 1331 |
while (i >= EXT_BLKSIZ) { |
| 1332 |
if (eb) |
| 1333 |
eb = eb->next; |
| 1334 |
else |
| 1335 |
break; |
| 1336 |
i -= EXT_BLKSIZ; |
| 1337 |
} |
| 1338 |
if (eb) |
| 1339 |
method |= 0x20, fidx = eb->exts[i]->index; |
| 1340 |
else |
| 1341 |
nasm_panic("unrecognised WRT value in obj_write_fixup"); |
| 1342 |
} |
| 1343 |
} |
| 1344 |
} |
| 1345 |
|
| 1346 |
forp = obj_byte(forp, method); |
| 1347 |
if (fidx != -1) |
| 1348 |
forp = obj_index(forp, fidx); |
| 1349 |
forp = obj_index(forp, tidx); |
| 1350 |
obj_commit(forp); |
| 1351 |
} |
| 1352 |
|
| 1353 |
static uint32_t check_segment_alignment(const uint64_t origalign) |
| 1354 |
{ |
| 1355 |
/* Supported alignment values */ |
| 1356 |
const uint32_t alignments = 1 | 2 | 4 | 16 | 256 | 4096; |
| 1357 |
uint32_t align = origalign; |
| 1358 |
|
| 1359 |
if (origalign && |
| 1360 |
(!is_power2(origalign) || origalign > alignments)) { |
| 1361 |
nasm_nonfatal("invalid alignment value %"PRIu64, origalign); |
| 1362 |
return 1; |
| 1363 |
} |
| 1364 |
|
| 1365 |
if (align == 0) |
| 1366 |
align = 1; |
| 1367 |
|
| 1368 |
if (align & alignments) |
| 1369 |
return align; /* All good! */ |
| 1370 |
|
| 1371 |
while (!(align & alignments)) |
| 1372 |
align <<= 1; |
| 1373 |
|
| 1374 |
nasm_warn(WARN_SECTION_ALIGNMENT_ROUNDED, |
| 1375 |
"alignment of %"PRIu64" not supported, using %"PRIu32, |
| 1376 |
origalign, align); |
| 1377 |
|
| 1378 |
return align; |
| 1379 |
} |
| 1380 |
|
| 1381 |
static int32_t obj_segment(char *name, int *bits) |
| 1382 |
{ |
| 1383 |
/* |
| 1384 |
* We call the label manager here to define a name for the new |
| 1385 |
* segment, and when our _own_ label-definition stub gets |
| 1386 |
* called in return, it should register the new segment name |
| 1387 |
* using the pointer it gets passed. That way we save memory, |
| 1388 |
* by sponging off the label manager. |
| 1389 |
*/ |
| 1390 |
nasm_debug(3, " obj_segment: < %s >, *bits=%d\n", name, *bits); |
| 1391 |
|
| 1392 |
if (!name) { |
| 1393 |
*bits = 16; |
| 1394 |
current_seg = NULL; |
| 1395 |
return first_seg; |
| 1396 |
} else { |
| 1397 |
struct Segment *seg; |
| 1398 |
struct Group *grp; |
| 1399 |
struct External **extp; |
| 1400 |
int obj_idx, i; |
| 1401 |
bool rn_error; |
| 1402 |
char *p; |
| 1403 |
|
| 1404 |
/* |
| 1405 |
* Look for segment attributes. |
| 1406 |
*/ |
| 1407 |
while (*name == '.') |
| 1408 |
name++; /* hack, but a documented one */ |
| 1409 |
p = nasm_skip_word(name); |
| 1410 |
if (*p) { |
| 1411 |
*p++ = '\0'; |
| 1412 |
p = nasm_skip_spaces(p); |
| 1413 |
} |
| 1414 |
|
| 1415 |
for (seg = seghead, obj_idx = 1; ; seg = seg->next, obj_idx++) { |
| 1416 |
if (!seg) |
| 1417 |
break; |
| 1418 |
|
| 1419 |
if (!strcmp(seg->name, name)) { |
| 1420 |
if (seg->pass_last_seen == pass_count()) { |
| 1421 |
if (*p) |
| 1422 |
nasm_warn(WARN_OTHER, "segment attributes specified on" |
| 1423 |
" redeclaration of segment: ignoring"); |
| 1424 |
} else { |
| 1425 |
/* Reissue alignment warning on this pass if necessary */ |
| 1426 |
if (seg->align < SEG_ABS) |
| 1427 |
check_segment_alignment(seg->origalign); |
| 1428 |
} |
| 1429 |
|
| 1430 |
if (seg->use32) |
| 1431 |
*bits = 32; |
| 1432 |
else |
| 1433 |
*bits = 16; |
| 1434 |
current_seg = seg; |
| 1435 |
seg->pass_last_seen = pass_count(); |
| 1436 |
return seg->index; |
| 1437 |
} |
| 1438 |
} |
| 1439 |
|
| 1440 |
*segtail = seg = nasm_malloc(sizeof(*seg)); |
| 1441 |
seg->next = NULL; |
| 1442 |
segtail = &seg->next; |
| 1443 |
seg->index = (any_segs ? seg_alloc() : first_seg); |
| 1444 |
seg->obj_index = obj_idx; |
| 1445 |
seg->grp = NULL; |
| 1446 |
any_segs = true; |
| 1447 |
seg->name = nasm_strdup(name); |
| 1448 |
seg->currentpos = 0; |
| 1449 |
if (ofmt == &of_obj) { |
| 1450 |
seg->align = 1; /* default for obj */ |
| 1451 |
seg->origalign = 1; /* default for obj */ |
| 1452 |
seg->use32 = false; /* default for obj */ |
| 1453 |
} else { |
| 1454 |
seg->align = 16; /* default for obj2 */ |
| 1455 |
seg->origalign = 16; /* default for obj2 */ |
| 1456 |
seg->use32 = true; /* default for obj2 */ |
| 1457 |
} |
| 1458 |
seg->combine = CMB_PUBLIC; /* default */ |
| 1459 |
seg->segclass = seg->overlay = NULL; |
| 1460 |
seg->pubhead = NULL; |
| 1461 |
seg->pubtail = &seg->pubhead; |
| 1462 |
seg->lochead = NULL; |
| 1463 |
seg->loctail = &seg->lochead; |
| 1464 |
seg->orp = obj_new(); |
| 1465 |
seg->orp->up = &(seg->orp); |
| 1466 |
seg->orp->ori = ori_ledata; |
| 1467 |
seg->orp->type = LEDATA; |
| 1468 |
seg->orp->parm[1] = obj_idx; |
| 1469 |
|
| 1470 |
/* |
| 1471 |
* Process the segment attributes. |
| 1472 |
*/ |
| 1473 |
while (*p) { |
| 1474 |
const char *q = p; |
| 1475 |
p = nasm_skip_word(p); |
| 1476 |
if (*p) { |
| 1477 |
*p++ = '\0'; |
| 1478 |
p = nasm_skip_spaces(p); |
| 1479 |
} |
| 1480 |
|
| 1481 |
/* |
| 1482 |
* `p' contains a segment attribute. |
| 1483 |
*/ |
| 1484 |
if (!nasm_stricmp(q, "private")) |
| 1485 |
seg->combine = CMB_PRIVATE; |
| 1486 |
else if (!nasm_stricmp(q, "public")) |
| 1487 |
seg->combine = CMB_PUBLIC; |
| 1488 |
else if (!nasm_stricmp(q, "common")) |
| 1489 |
seg->combine = CMB_COMMON; |
| 1490 |
else if (!nasm_stricmp(q, "stack")) |
| 1491 |
seg->combine = CMB_STACK; |
| 1492 |
else if (!nasm_stricmp(q, "use16")) |
| 1493 |
seg->use32 = false; |
| 1494 |
else if (!nasm_stricmp(q, "use32")) |
| 1495 |
seg->use32 = true; |
| 1496 |
else if (!nasm_stricmp(q, "flat")) { |
| 1497 |
/* |
| 1498 |
* This segment is an OS/2 FLAT segment. That means |
| 1499 |
* that its default group is group FLAT, even if |
| 1500 |
* the group FLAT does not explicitly _contain_ the |
| 1501 |
* segment. |
| 1502 |
* |
| 1503 |
* When we see this, we must create the group |
| 1504 |
* `FLAT', containing no segments, if it does not |
| 1505 |
* already exist; then we must set the default |
| 1506 |
* group of this segment to be the FLAT group. |
| 1507 |
*/ |
| 1508 |
struct Group *grp; |
| 1509 |
for (grp = grphead; grp; grp = grp->next) |
| 1510 |
if (!strcmp(grp->name, "FLAT")) |
| 1511 |
break; |
| 1512 |
if (!grp) { |
| 1513 |
obj_directive(D_GROUP, "FLAT"); |
| 1514 |
for (grp = grphead; grp; grp = grp->next) |
| 1515 |
if (!strcmp(grp->name, "FLAT")) |
| 1516 |
break; |
| 1517 |
if (!grp) |
| 1518 |
nasm_panic("failure to define FLAT?!"); |
| 1519 |
} |
| 1520 |
seg->grp = grp; |
| 1521 |
} else if (!nasm_strnicmp(q, "class=", 6)) |
| 1522 |
seg->segclass = nasm_strdup(q + 6); |
| 1523 |
else if (!nasm_strnicmp(q, "overlay=", 8)) |
| 1524 |
seg->overlay = nasm_strdup(q + 8); |
| 1525 |
else if (!nasm_strnicmp(q, "align=", 6)) { |
| 1526 |
uint64_t n = readnum(q + 6, &rn_error); |
| 1527 |
if (rn_error) { |
| 1528 |
nasm_nonfatal("segment alignment should be numeric"); |
| 1529 |
n = 1; |
| 1530 |
} |
| 1531 |
seg->origalign = n; |
| 1532 |
seg->align = check_segment_alignment(n); |
| 1533 |
} else if (!nasm_strnicmp(q, "absolute=", 9)) { |
| 1534 |
uint64_t n = readnum(q + 9, &rn_error); |
| 1535 |
if (rn_error) { |
| 1536 |
nasm_nonfatal("argument to `absolute' segment" |
| 1537 |
" attribute should be numeric"); |
| 1538 |
n = 0; |
| 1539 |
} else if (n >= SEG_ABS) { |
| 1540 |
/* Probably could even be max 64K? */ |
| 1541 |
nasm_nonfatal("unsupported `absolute' segment number %#"PRIx64, n); |
| 1542 |
n = 0; |
| 1543 |
} |
| 1544 |
seg->align = SEG_ABS + n; |
| 1545 |
} |
| 1546 |
} |
| 1547 |
|
| 1548 |
if (!seg->use32 && seg->grp && !strcmp(seg->grp->name, "FLAT")) |
| 1549 |
nasm_panic("wrong combination of USE16(16-bit segment) and FLAT"); |
| 1550 |
|
| 1551 |
if (ofmt == &of_obj2) { |
| 1552 |
if (seg->use32 && !seg->grp) { |
| 1553 |
struct Group *grp; |
| 1554 |
for (grp = grphead; grp; grp = grp->next) |
| 1555 |
if (!strcmp(grp->name, "FLAT")) |
| 1556 |
break; |
| 1557 |
if (!grp) { |
| 1558 |
obj_directive(D_GROUP, "FLAT"); |
| 1559 |
for (grp = grphead; grp; grp = grp->next) |
| 1560 |
if (!strcmp(grp->name, "FLAT")) |
| 1561 |
break; |
| 1562 |
if (!grp) |
| 1563 |
nasm_panic("failure to define FLAT?!"); |
| 1564 |
} |
| 1565 |
seg->grp = grp; |
| 1566 |
} |
| 1567 |
|
| 1568 |
if (!seg->segclass) { |
| 1569 |
const char *segclass = get_default_class(seg->name); |
| 1570 |
|
| 1571 |
if (segclass) |
| 1572 |
seg->segclass = nasm_strdup(segclass); |
| 1573 |
} |
| 1574 |
} |
| 1575 |
|
| 1576 |
/* We need to know whenever we have at least one 32-bit segment */ |
| 1577 |
obj_use32 |= seg->use32; |
| 1578 |
|
| 1579 |
/* |
| 1580 |
* Trying to create a symbol on the last pass will end in tears. |
| 1581 |
* This can happen if there is no SEGMENT directive, and there |
| 1582 |
* are no labels. |
| 1583 |
*/ |
| 1584 |
if (!pass_final()) { |
| 1585 |
obj_seg_needs_update = seg; |
| 1586 |
if (seg->align >= SEG_ABS) |
| 1587 |
define_label(name, NO_SEG, seg->align - SEG_ABS, false); |
| 1588 |
else |
| 1589 |
define_label(name, seg->index + 1, 0L, false); |
| 1590 |
obj_seg_needs_update = NULL; |
| 1591 |
} |
| 1592 |
|
| 1593 |
/* |
| 1594 |
* See if this segment is defined in any groups. |
| 1595 |
*/ |
| 1596 |
for (grp = grphead; grp; grp = grp->next) { |
| 1597 |
for (i = grp->nindices; i < grp->nentries; i++) { |
| 1598 |
if (!strcmp(grp->segs[i].name, seg->name)) { |
| 1599 |
nasm_free(grp->segs[i].name); |
| 1600 |
grp->segs[i] = grp->segs[grp->nindices]; |
| 1601 |
grp->segs[grp->nindices++].index = seg->obj_index; |
| 1602 |
/* |
| 1603 |
* The group FLAT is a pseudo group. Therefore, it is |
| 1604 |
* allowed to redefine a segment in the group FLAT as |
| 1605 |
* other group. |
| 1606 |
*/ |
| 1607 |
if (seg->grp && strcmp(seg->grp->name, "FLAT")) |
| 1608 |
nasm_warn(WARN_OTHER, "segment `%s' is already part of" |
| 1609 |
" a group: first one takes precedence", |
| 1610 |
seg->name); |
| 1611 |
else |
| 1612 |
seg->grp = grp; |
| 1613 |
} |
| 1614 |
} |
| 1615 |
} |
| 1616 |
|
| 1617 |
/* |
| 1618 |
* Walk through the list of externals with unresolved |
| 1619 |
* default-WRT clauses, and resolve any that point at this |
| 1620 |
* segment. |
| 1621 |
*/ |
| 1622 |
extp = &dws; |
| 1623 |
while (*extp) { |
| 1624 |
if ((*extp)->defwrt_type == DEFWRT_STRING && |
| 1625 |
!strcmp((*extp)->defwrt_ptr.string, seg->name)) { |
| 1626 |
nasm_free((*extp)->defwrt_ptr.string); |
| 1627 |
(*extp)->defwrt_type = DEFWRT_SEGMENT; |
| 1628 |
(*extp)->defwrt_ptr.seg = seg; |
| 1629 |
*extp = (*extp)->next_dws; |
| 1630 |
} else |
| 1631 |
extp = &(*extp)->next_dws; |
| 1632 |
} |
| 1633 |
|
| 1634 |
if (seg->use32) |
| 1635 |
*bits = 32; |
| 1636 |
else |
| 1637 |
*bits = 16; |
| 1638 |
current_seg = seg; |
| 1639 |
return seg->index; |
| 1640 |
} |
| 1641 |
} |
| 1642 |
|
| 1643 |
static enum directive_result |
| 1644 |
obj_directive(enum directive directive, char *value) |
| 1645 |
{ |
| 1646 |
switch (directive) { |
| 1647 |
case D_GROUP: |
| 1648 |
/* Is pass_first() really correct? */ |
| 1649 |
if (value && pass_first()) { |
| 1650 |
char *p, *q, *v; |
| 1651 |
struct Group *grp; |
| 1652 |
struct Segment *seg; |
| 1653 |
struct External **extp; |
| 1654 |
int obj_idx; |
| 1655 |
const char *segname; |
| 1656 |
int i; |
| 1657 |
|
| 1658 |
q = value; |
| 1659 |
while (*q == '.') |
| 1660 |
q++; /* hack, but a documented one */ |
| 1661 |
v = q; |
| 1662 |
while (*q && !nasm_isspace(*q)) |
| 1663 |
q++; |
| 1664 |
if (nasm_isspace(*q)) { |
| 1665 |
*q++ = '\0'; |
| 1666 |
while (*q && nasm_isspace(*q)) |
| 1667 |
q++; |
| 1668 |
} |
| 1669 |
/* |
| 1670 |
* Here we used to sanity-check the group directive to |
| 1671 |
* ensure nobody tried to declare a group containing no |
| 1672 |
* segments. However, OS/2 does this as standard |
| 1673 |
* practice, so the sanity check has been removed. |
| 1674 |
* |
| 1675 |
* if (!*q) { |
| 1676 |
* nasm_error(ERR_NONFATAL,"GROUP directive contains no segments"); |
| 1677 |
* return DIRR_ERROR; |
| 1678 |
* } |
| 1679 |
*/ |
| 1680 |
|
| 1681 |
obj_idx = 1; |
| 1682 |
for (grp = grphead; grp; grp = grp->next) { |
| 1683 |
obj_idx++; |
| 1684 |
if (!strcmp(grp->name, v)) { |
| 1685 |
break; |
| 1686 |
} |
| 1687 |
} |
| 1688 |
|
| 1689 |
if (!grp) { |
| 1690 |
*grptail = grp = nasm_malloc(sizeof(*grp)); |
| 1691 |
grp->next = NULL; |
| 1692 |
grptail = &grp->next; |
| 1693 |
grp->index = seg_alloc(); |
| 1694 |
grp->obj_index = obj_idx; |
| 1695 |
grp->nindices = grp->nentries = 0; |
| 1696 |
grp->name = NULL; |
| 1697 |
|
| 1698 |
obj_grp_needs_update = grp; |
| 1699 |
backend_label(v, grp->index + 1, 0L); |
| 1700 |
obj_grp_needs_update = NULL; |
| 1701 |
} |
| 1702 |
|
| 1703 |
while (*q) { |
| 1704 |
p = q; |
| 1705 |
while (*q && !nasm_isspace(*q)) |
| 1706 |
q++; |
| 1707 |
if (nasm_isspace(*q)) { |
| 1708 |
*q++ = '\0'; |
| 1709 |
while (*q && nasm_isspace(*q)) |
| 1710 |
q++; |
| 1711 |
} |
| 1712 |
/* |
| 1713 |
* Now p contains a segment name. Find it. |
| 1714 |
*/ |
| 1715 |
for (i = 0; i < grp->nentries; i++) { |
| 1716 |
if (i < grp->nindices) { |
| 1717 |
segname = NULL; /* make compiler happy */ |
| 1718 |
for (seg = seghead; seg; seg = seg->next) { |
| 1719 |
if (grp->segs[i].index == seg->obj_index) { |
| 1720 |
segname = seg->name; |
| 1721 |
break; |
| 1722 |
} |
| 1723 |
} |
| 1724 |
} |
| 1725 |
else |
| 1726 |
segname = grp->segs[i].name; |
| 1727 |
/* |
| 1728 |
* See if this segment is defined in this group. |
| 1729 |
*/ |
| 1730 |
if (!strcmp(segname, p)) |
| 1731 |
break; |
| 1732 |
} |
| 1733 |
if (i < grp->nentries) { |
| 1734 |
/* |
| 1735 |
* We have already this segment. Skip. |
| 1736 |
*/ |
| 1737 |
continue; |
| 1738 |
} |
| 1739 |
for (seg = seghead; seg; seg = seg->next) |
| 1740 |
if (!strcmp(seg->name, p)) |
| 1741 |
break; |
| 1742 |
if (seg) { |
| 1743 |
/* |
| 1744 |
* We have a segment index. Shift a name entry |
| 1745 |
* to the end of the array to make room. |
| 1746 |
*/ |
| 1747 |
grp->segs[grp->nentries++] = grp->segs[grp->nindices]; |
| 1748 |
grp->segs[grp->nindices++].index = seg->obj_index; |
| 1749 |
/* |
| 1750 |
* The group FLAT is a pseudo group. Therefore, it is |
| 1751 |
* allowed to redefine a segment in the group FLAT as |
| 1752 |
* other group. |
| 1753 |
*/ |
| 1754 |
if (seg->grp && strcmp(seg->grp->name, "FLAT")) |
| 1755 |
nasm_warn(WARN_OTHER, "segment `%s' is already part of" |
| 1756 |
" a group: first one takes precedence", |
| 1757 |
seg->name); |
| 1758 |
else |
| 1759 |
seg->grp = grp; |
| 1760 |
} else { |
| 1761 |
/* |
| 1762 |
* We have an as-yet undefined segment. |
| 1763 |
* Remember its name, for later. |
| 1764 |
*/ |
| 1765 |
grp->segs[grp->nentries++].name = nasm_strdup(p); |
| 1766 |
} |
| 1767 |
} |
| 1768 |
|
| 1769 |
/* |
| 1770 |
* Walk through the list of externals with unresolved |
| 1771 |
* default-WRT clauses, and resolve any that point at |
| 1772 |
* this group. |
| 1773 |
*/ |
| 1774 |
extp = &dws; |
| 1775 |
while (*extp) { |
| 1776 |
if ((*extp)->defwrt_type == DEFWRT_STRING && |
| 1777 |
!strcmp((*extp)->defwrt_ptr.string, grp->name)) { |
| 1778 |
nasm_free((*extp)->defwrt_ptr.string); |
| 1779 |
(*extp)->defwrt_type = DEFWRT_GROUP; |
| 1780 |
(*extp)->defwrt_ptr.grp = grp; |
| 1781 |
*extp = (*extp)->next_dws; |
| 1782 |
} else |
| 1783 |
extp = &(*extp)->next_dws; |
| 1784 |
} |
| 1785 |
} |
| 1786 |
return DIRR_OK; |
| 1787 |
|
| 1788 |
case D_UPPERCASE: |
| 1789 |
if (value) |
| 1790 |
get_boolean_option(value, &obj_uppercase); |
| 1791 |
return DIRR_OK; |
| 1792 |
|
| 1793 |
case D_IMPORT: |
| 1794 |
/* Is pass_first() really correct? */ |
| 1795 |
if (value && pass_first()) { |
| 1796 |
char *q, *extname, *libname, *impname; |
| 1797 |
|
| 1798 |
extname = q = value; |
| 1799 |
while (*q && !nasm_isspace(*q)) |
| 1800 |
q++; |
| 1801 |
if (nasm_isspace(*q)) { |
| 1802 |
*q++ = '\0'; |
| 1803 |
while (*q && nasm_isspace(*q)) |
| 1804 |
q++; |
| 1805 |
} |
| 1806 |
|
| 1807 |
libname = q; |
| 1808 |
while (*q && !nasm_isspace(*q)) |
| 1809 |
q++; |
| 1810 |
if (nasm_isspace(*q)) { |
| 1811 |
*q++ = '\0'; |
| 1812 |
while (*q && nasm_isspace(*q)) |
| 1813 |
q++; |
| 1814 |
} |
| 1815 |
|
| 1816 |
impname = q; |
| 1817 |
|
| 1818 |
if (!*extname || !*libname) |
| 1819 |
nasm_nonfatal("`import' directive requires symbol name" |
| 1820 |
" and library name"); |
| 1821 |
else { |
| 1822 |
struct ImpDef *imp; |
| 1823 |
bool err = false; |
| 1824 |
|
| 1825 |
imp = *imptail = nasm_malloc(sizeof(struct ImpDef)); |
| 1826 |
imptail = &imp->next; |
| 1827 |
imp->next = NULL; |
| 1828 |
imp->extname = nasm_strdup(extname); |
| 1829 |
imp->libname = nasm_strdup(libname); |
| 1830 |
imp->impindex = readnum(impname, &err); |
| 1831 |
if (!*impname || err) |
| 1832 |
imp->impname = nasm_strdup(impname); |
| 1833 |
else |
| 1834 |
imp->impname = NULL; |
| 1835 |
} |
| 1836 |
} |
| 1837 |
return DIRR_OK; |
| 1838 |
|
| 1839 |
case D_EXPORT: |
| 1840 |
/* Is pass_first() really correct? */ |
| 1841 |
if (value && pass_first()) { |
| 1842 |
char *q, *extname, *intname, *v; |
| 1843 |
struct ExpDef *export; |
| 1844 |
int flags = 0; |
| 1845 |
unsigned int ordinal = 0; |
| 1846 |
|
| 1847 |
intname = q = value; |
| 1848 |
while (*q && !nasm_isspace(*q)) |
| 1849 |
q++; |
| 1850 |
if (nasm_isspace(*q)) { |
| 1851 |
*q++ = '\0'; |
| 1852 |
while (*q && nasm_isspace(*q)) |
| 1853 |
q++; |
| 1854 |
} |
| 1855 |
|
| 1856 |
extname = q; |
| 1857 |
while (*q && !nasm_isspace(*q)) |
| 1858 |
q++; |
| 1859 |
if (nasm_isspace(*q)) { |
| 1860 |
*q++ = '\0'; |
| 1861 |
while (*q && nasm_isspace(*q)) |
| 1862 |
q++; |
| 1863 |
} |
| 1864 |
|
| 1865 |
if (!*intname) { |
| 1866 |
nasm_nonfatal("`export' directive requires export name"); |
| 1867 |
return DIRR_OK; |
| 1868 |
} |
| 1869 |
if (!*extname) { |
| 1870 |
extname = intname; |
| 1871 |
intname = ""; |
| 1872 |
} |
| 1873 |
while (*q) { |
| 1874 |
v = q; |
| 1875 |
while (*q && !nasm_isspace(*q)) |
| 1876 |
q++; |
| 1877 |
if (nasm_isspace(*q)) { |
| 1878 |
*q++ = '\0'; |
| 1879 |
while (*q && nasm_isspace(*q)) |
| 1880 |
q++; |
| 1881 |
} |
| 1882 |
if (!nasm_stricmp(v, "resident")) |
| 1883 |
flags |= EXPDEF_FLAG_RESIDENT; |
| 1884 |
else if (!nasm_stricmp(v, "nodata")) |
| 1885 |
flags |= EXPDEF_FLAG_NODATA; |
| 1886 |
else if (!nasm_strnicmp(v, "parm=", 5)) { |
| 1887 |
bool err = false; |
| 1888 |
flags |= EXPDEF_MASK_PARMCNT & readnum(v + 5, &err); |
| 1889 |
if (err) { |
| 1890 |
nasm_nonfatal("value `%s' for `parm' is non-numeric", v + 5); |
| 1891 |
return DIRR_ERROR; |
| 1892 |
} |
| 1893 |
} else { |
| 1894 |
bool err = false; |
| 1895 |
ordinal = readnum(v, &err); |
| 1896 |
if (err) { |
| 1897 |
nasm_nonfatal("unrecognised export qualifier `%s'", v); |
| 1898 |
return DIRR_ERROR; |
| 1899 |
} |
| 1900 |
flags |= EXPDEF_FLAG_ORDINAL; |
| 1901 |
} |
| 1902 |
} |
| 1903 |
|
| 1904 |
export = *exptail = nasm_malloc(sizeof(struct ExpDef)); |
| 1905 |
exptail = &export->next; |
| 1906 |
export->next = NULL; |
| 1907 |
export->extname = nasm_strdup(extname); |
| 1908 |
export->intname = nasm_strdup(intname); |
| 1909 |
export->ordinal = ordinal; |
| 1910 |
export->flags = flags; |
| 1911 |
} |
| 1912 |
return DIRR_OK; |
| 1913 |
|
| 1914 |
default: |
| 1915 |
return DIRR_UNKNOWN; |
| 1916 |
} |
| 1917 |
} |
| 1918 |
|
| 1919 |
static void obj_sectalign(int32_t seg, unsigned int value) |
| 1920 |
{ |
| 1921 |
struct Segment *s; |
| 1922 |
|
| 1923 |
list_for_each(s, seghead) { |
| 1924 |
if (s->index == seg) |
| 1925 |
break; |
| 1926 |
} |
| 1927 |
|
| 1928 |
/* |
| 1929 |
* it should not be too big value |
| 1930 |
* and applied on non-absolute sections |
| 1931 |
*/ |
| 1932 |
if (!s || !is_power2(value) || |
| 1933 |
value > 4096 || s->align >= SEG_ABS) |
| 1934 |
return; |
| 1935 |
|
| 1936 |
/* |
| 1937 |
* FIXME: No code duplication please |
| 1938 |
* consider making helper for this |
| 1939 |
* mapping since section handler has |
| 1940 |
* to do the same |
| 1941 |
*/ |
| 1942 |
switch (value) { |
| 1943 |
case 8: |
| 1944 |
value = 16; |
| 1945 |
break; |
| 1946 |
case 32: |
| 1947 |
case 64: |
| 1948 |
case 128: |
| 1949 |
value = 256; |
| 1950 |
break; |
| 1951 |
case 512: |
| 1952 |
case 1024: |
| 1953 |
case 2048: |
| 1954 |
value = 4096; |
| 1955 |
break; |
| 1956 |
} |
| 1957 |
|
| 1958 |
if (s->align < (int)value) |
| 1959 |
s->align = value; |
| 1960 |
} |
| 1961 |
|
| 1962 |
static int32_t obj_segbase(int32_t segment) |
| 1963 |
{ |
| 1964 |
struct Segment *seg; |
| 1965 |
|
| 1966 |
/* |
| 1967 |
* Find the segment in our list. |
| 1968 |
*/ |
| 1969 |
for (seg = seghead; seg; seg = seg->next) |
| 1970 |
if (seg->index == segment - 1) |
| 1971 |
break; |
| 1972 |
|
| 1973 |
if (!seg) { |
| 1974 |
/* |
| 1975 |
* Might be an external with a default WRT. |
| 1976 |
*/ |
| 1977 |
int32_t i = segment / 2; |
| 1978 |
struct ExtBack *eb = ebhead; |
| 1979 |
struct External *e; |
| 1980 |
|
| 1981 |
while (i >= EXT_BLKSIZ) { |
| 1982 |
if (eb) |
| 1983 |
eb = eb->next; |
| 1984 |
else |
| 1985 |
break; |
| 1986 |
i -= EXT_BLKSIZ; |
| 1987 |
} |
| 1988 |
if (eb) { |
| 1989 |
e = eb->exts[i]; |
| 1990 |
if (!e) { |
| 1991 |
/* Not available yet, probably a forward reference */ |
| 1992 |
nasm_assert(!pass_final()); |
| 1993 |
return NO_SEG; |
| 1994 |
} |
| 1995 |
|
| 1996 |
switch (e->defwrt_type) { |
| 1997 |
case DEFWRT_NONE: |
| 1998 |
return segment; /* fine */ |
| 1999 |
case DEFWRT_SEGMENT: |
| 2000 |
return e->defwrt_ptr.seg->index + 1; |
| 2001 |
case DEFWRT_GROUP: |
| 2002 |
return e->defwrt_ptr.grp->index + 1; |
| 2003 |
default: |
| 2004 |
return NO_SEG; /* can't tell what it is */ |
| 2005 |
} |
| 2006 |
} |
| 2007 |
|
| 2008 |
return segment; /* not one of ours - leave it alone */ |
| 2009 |
} |
| 2010 |
|
| 2011 |
if (seg->align >= SEG_ABS) |
| 2012 |
return seg->align; /* absolute segment */ |
| 2013 |
if (seg->grp) |
| 2014 |
return seg->grp->index + 1; /* grouped segment */ |
| 2015 |
|
| 2016 |
return segment; /* no special treatment */ |
| 2017 |
} |
| 2018 |
|
| 2019 |
/* Get a file timestamp in MS-DOS format */ |
| 2020 |
static uint32_t obj_file_timestamp(const char *pathname) |
| 2021 |
{ |
| 2022 |
time_t t; |
| 2023 |
const struct tm *lt; |
| 2024 |
|
| 2025 |
if (!nasm_file_time(&t, pathname)) |
| 2026 |
return 0; |
| 2027 |
|
| 2028 |
lt = localtime(&t); |
| 2029 |
if (!lt) |
| 2030 |
return 0; |
| 2031 |
|
| 2032 |
if (lt->tm_year < 80 || lt->tm_year > 207) |
| 2033 |
return 0; /* Only years 1980-2107 representable */ |
| 2034 |
|
| 2035 |
return |
| 2036 |
((uint32_t)lt->tm_sec >> 1) + |
| 2037 |
((uint32_t)lt->tm_min << 5) + |
| 2038 |
((uint32_t)lt->tm_hour << 11) + |
| 2039 |
((uint32_t)lt->tm_mday << 16) + |
| 2040 |
(((uint32_t)lt->tm_mon + 1) << 21) + |
| 2041 |
(((uint32_t)lt->tm_year - 80) << 25); |
| 2042 |
} |
| 2043 |
|
| 2044 |
static void obj_write_file(void) |
| 2045 |
{ |
| 2046 |
struct Segment *seg, *entry_seg_ptr = 0; |
| 2047 |
struct FileName *fn; |
| 2048 |
struct LineNumber *ln; |
| 2049 |
struct Group *grp; |
| 2050 |
struct Public *pub, *loc; |
| 2051 |
struct External *ext; |
| 2052 |
struct ImpDef *imp; |
| 2053 |
struct ExpDef *export; |
| 2054 |
int lname_idx; |
| 2055 |
ObjRecord *orp; |
| 2056 |
const struct strlist_entry *depfile; |
| 2057 |
const bool debuginfo = (dfmt == &borland_debug_form); |
| 2058 |
|
| 2059 |
/* |
| 2060 |
* Write the THEADR module header. |
| 2061 |
*/ |
| 2062 |
orp = obj_new(); |
| 2063 |
orp->type = THEADR; |
| 2064 |
obj_name(orp, obj_infile); |
| 2065 |
obj_emit2(orp); |
| 2066 |
|
| 2067 |
/* |
| 2068 |
* Write the NASM boast comment. |
| 2069 |
*/ |
| 2070 |
orp->type = COMENT; |
| 2071 |
obj_rword(orp, dTRANSL); |
| 2072 |
obj_name(orp, nasm_comment()); |
| 2073 |
obj_emit2(orp); |
| 2074 |
|
| 2075 |
/* |
| 2076 |
* Output file dependency information |
| 2077 |
*/ |
| 2078 |
if (!obj_nodepend && depend_list) { |
| 2079 |
strlist_for_each(depfile, depend_list) { |
| 2080 |
uint32_t ts; |
| 2081 |
|
| 2082 |
ts = obj_file_timestamp(depfile->str); |
| 2083 |
if (ts) { |
| 2084 |
orp->type = COMENT; |
| 2085 |
obj_rword(orp, dDEPFILE); |
| 2086 |
obj_dword(orp, ts); |
| 2087 |
obj_name(orp, depfile->str); |
| 2088 |
obj_emit2(orp); |
| 2089 |
} |
| 2090 |
} |
| 2091 |
} |
| 2092 |
|
| 2093 |
orp->type = COMENT; |
| 2094 |
/* |
| 2095 |
* Write the IMPDEF records, if any. |
| 2096 |
*/ |
| 2097 |
for (imp = imphead; imp; imp = imp->next) { |
| 2098 |
obj_rword(orp, dOMFEXT); |
| 2099 |
obj_byte(orp, 1); /* subfunction 1: IMPDEF */ |
| 2100 |
if (imp->impname) |
| 2101 |
obj_byte(orp, 0); /* import by name */ |
| 2102 |
else |
| 2103 |
obj_byte(orp, 1); /* import by ordinal */ |
| 2104 |
obj_name(orp, imp->extname); |
| 2105 |
obj_name(orp, imp->libname); |
| 2106 |
if (imp->impname) |
| 2107 |
obj_name(orp, imp->impname); |
| 2108 |
else |
| 2109 |
obj_word(orp, imp->impindex); |
| 2110 |
obj_emit2(orp); |
| 2111 |
} |
| 2112 |
|
| 2113 |
/* |
| 2114 |
* Write the EXPDEF records, if any. |
| 2115 |
*/ |
| 2116 |
for (export = exphead; export; export = export->next) { |
| 2117 |
obj_rword(orp, dOMFEXT); |
| 2118 |
obj_byte(orp, 2); /* subfunction 2: EXPDEF */ |
| 2119 |
obj_byte(orp, export->flags); |
| 2120 |
obj_name(orp, export->extname); |
| 2121 |
obj_name(orp, export->intname); |
| 2122 |
if (export->flags & EXPDEF_FLAG_ORDINAL) |
| 2123 |
obj_word(orp, export->ordinal); |
| 2124 |
obj_emit2(orp); |
| 2125 |
} |
| 2126 |
|
| 2127 |
/* we're using extended OMF if we put in debug info */ |
| 2128 |
if (debuginfo) { |
| 2129 |
orp->type = COMENT; |
| 2130 |
obj_rword(orp, dEXTENDED); |
| 2131 |
obj_emit2(orp); |
| 2132 |
} |
| 2133 |
|
| 2134 |
/* |
| 2135 |
* Write the first LNAMES record, containing LNAME one, which |
| 2136 |
* is null. Also initialize the LNAME counter. |
| 2137 |
*/ |
| 2138 |
orp->type = LNAMES; |
| 2139 |
obj_byte(orp, 0); |
| 2140 |
lname_idx = 1; |
| 2141 |
/* |
| 2142 |
* Write some LNAMES for the segment names |
| 2143 |
*/ |
| 2144 |
for (seg = seghead; seg; seg = seg->next) { |
| 2145 |
orp = obj_name(orp, seg->name); |
| 2146 |
if (seg->segclass) |
| 2147 |
orp = obj_name(orp, seg->segclass); |
| 2148 |
if (seg->overlay) |
| 2149 |
orp = obj_name(orp, seg->overlay); |
| 2150 |
obj_commit(orp); |
| 2151 |
} |
| 2152 |
/* |
| 2153 |
* Write some LNAMES for the group names |
| 2154 |
*/ |
| 2155 |
for (grp = grphead; grp; grp = grp->next) { |
| 2156 |
orp = obj_name(orp, grp->name); |
| 2157 |
obj_commit(orp); |
| 2158 |
} |
| 2159 |
obj_emit(orp); |
| 2160 |
|
| 2161 |
/* |
| 2162 |
* Write the SEGDEF records. |
| 2163 |
*/ |
| 2164 |
orp->type = SEGDEF; |
| 2165 |
for (seg = seghead; seg; seg = seg->next) { |
| 2166 |
int acbp; |
| 2167 |
uint32_t seglen = seg->currentpos; |
| 2168 |
|
| 2169 |
acbp = (seg->combine << 2); /* C field */ |
| 2170 |
|
| 2171 |
if (seg->use32) |
| 2172 |
acbp |= 0x01; /* P bit is Use32 flag */ |
| 2173 |
else if (seglen == 0x10000L) { |
| 2174 |
seglen = 0; /* This special case may be needed for old linkers */ |
| 2175 |
acbp |= 0x02; /* B bit */ |
| 2176 |
} |
| 2177 |
|
| 2178 |
/* A field */ |
| 2179 |
if (seg->align >= SEG_ABS) |
| 2180 |
/* acbp |= 0x00 */ ; |
| 2181 |
else if (seg->align >= 4096) { |
| 2182 |
if (seg->align > 4096) |
| 2183 |
nasm_nonfatal("segment `%s' requires more alignment" |
| 2184 |
" than OBJ format supports", seg->name); |
| 2185 |
acbp |= 0xC0; /* PharLap extension */ |
| 2186 |
} else if (seg->align >= 256) { |
| 2187 |
acbp |= 0x80; |
| 2188 |
} else if (seg->align >= 16) { |
| 2189 |
acbp |= 0x60; |
| 2190 |
} else if (seg->align >= 4) { |
| 2191 |
acbp |= 0xA0; |
| 2192 |
} else if (seg->align >= 2) { |
| 2193 |
acbp |= 0x40; |
| 2194 |
} else |
| 2195 |
acbp |= 0x20; |
| 2196 |
|
| 2197 |
obj_byte(orp, acbp); |
| 2198 |
if (seg->align & SEG_ABS) { |
| 2199 |
obj_x(orp, seg->align - SEG_ABS); /* Frame */ |
| 2200 |
obj_byte(orp, 0); /* Offset */ |
| 2201 |
} |
| 2202 |
obj_x(orp, seglen); |
| 2203 |
obj_index(orp, ++lname_idx); |
| 2204 |
obj_index(orp, seg->segclass ? ++lname_idx : 1); |
| 2205 |
obj_index(orp, seg->overlay ? ++lname_idx : 1); |
| 2206 |
obj_emit2(orp); |
| 2207 |
} |
| 2208 |
|
| 2209 |
/* |
| 2210 |
* Write the GRPDEF records. |
| 2211 |
*/ |
| 2212 |
orp->type = GRPDEF; |
| 2213 |
for (grp = grphead; grp; grp = grp->next) { |
| 2214 |
int i; |
| 2215 |
|
| 2216 |
if (grp->nindices != grp->nentries) { |
| 2217 |
for (i = grp->nindices; i < grp->nentries; i++) { |
| 2218 |
nasm_nonfatal("group `%s' contains undefined segment" |
| 2219 |
" `%s'", grp->name, grp->segs[i].name); |
| 2220 |
nasm_free(grp->segs[i].name); |
| 2221 |
grp->segs[i].name = NULL; |
| 2222 |
} |
| 2223 |
} |
| 2224 |
obj_index(orp, ++lname_idx); |
| 2225 |
for (i = 0; i < grp->nindices; i++) { |
| 2226 |
obj_byte(orp, 0xFF); |
| 2227 |
obj_index(orp, grp->segs[i].index); |
| 2228 |
} |
| 2229 |
obj_emit2(orp); |
| 2230 |
} |
| 2231 |
|
| 2232 |
/* |
| 2233 |
* Write the PUBDEF records: first the ones in the segments, |
| 2234 |
* then the far-absolutes. |
| 2235 |
*/ |
| 2236 |
orp->type = PUBDEF; |
| 2237 |
orp->ori = ori_pubdef; |
| 2238 |
for (seg = seghead; seg; seg = seg->next) { |
| 2239 |
orp->parm[0] = seg->grp ? seg->grp->obj_index : 0; |
| 2240 |
orp->parm[1] = seg->obj_index; |
| 2241 |
for (pub = seg->pubhead; pub; pub = pub->next) { |
| 2242 |
orp = obj_name(orp, pub->name); |
| 2243 |
orp = obj_x(orp, pub->offset); |
| 2244 |
orp = obj_byte(orp, 0); /* type index */ |
| 2245 |
obj_commit(orp); |
| 2246 |
} |
| 2247 |
obj_emit(orp); |
| 2248 |
} |
| 2249 |
orp->parm[0] = 0; |
| 2250 |
orp->parm[1] = 0; |
| 2251 |
for (pub = fpubhead; pub; pub = pub->next) { /* pub-crawl :-) */ |
| 2252 |
if (orp->parm[2] != (uint32_t)pub->segment) { |
| 2253 |
obj_emit(orp); |
| 2254 |
orp->parm[2] = pub->segment; |
| 2255 |
} |
| 2256 |
orp = obj_name(orp, pub->name); |
| 2257 |
orp = obj_x(orp, pub->offset); |
| 2258 |
orp = obj_byte(orp, 0); /* type index */ |
| 2259 |
obj_commit(orp); |
| 2260 |
} |
| 2261 |
obj_emit(orp); |
| 2262 |
|
| 2263 |
/* |
| 2264 |
* Write the EXTDEF and COMDEF records, in order. |
| 2265 |
*/ |
| 2266 |
orp->ori = ori_null; |
| 2267 |
for (ext = exthead; ext; ext = ext->next) { |
| 2268 |
if (ext->commonsize == 0) { |
| 2269 |
if (orp->type != EXTDEF) { |
| 2270 |
obj_emit(orp); |
| 2271 |
orp->type = EXTDEF; |
| 2272 |
} |
| 2273 |
orp = obj_name(orp, ext->name); |
| 2274 |
orp = obj_index(orp, 0); |
| 2275 |
} else { |
| 2276 |
if (orp->type != COMDEF) { |
| 2277 |
obj_emit(orp); |
| 2278 |
orp->type = COMDEF; |
| 2279 |
} |
| 2280 |
orp = obj_name(orp, ext->name); |
| 2281 |
orp = obj_index(orp, 0); |
| 2282 |
if (ext->commonelem) { |
| 2283 |
orp = obj_byte(orp, 0x61); /* far communal */ |
| 2284 |
orp = obj_value(orp, (ext->commonsize / ext->commonelem)); |
| 2285 |
orp = obj_value(orp, ext->commonelem); |
| 2286 |
} else { |
| 2287 |
orp = obj_byte(orp, 0x62); /* near communal */ |
| 2288 |
orp = obj_value(orp, ext->commonsize); |
| 2289 |
} |
| 2290 |
} |
| 2291 |
obj_commit(orp); |
| 2292 |
} |
| 2293 |
obj_emit(orp); |
| 2294 |
|
| 2295 |
/* |
| 2296 |
* Write a COMENT record stating that the linker's first pass |
| 2297 |
* may stop processing at this point. Exception is if our |
| 2298 |
* MODEND record specifies a start point, in which case, |
| 2299 |
* according to some variants of the documentation, this COMENT |
| 2300 |
* should be omitted. So we'll omit it just in case. |
| 2301 |
* But, TASM puts it in all the time so if we are using |
| 2302 |
* TASM debug stuff we are putting it in |
| 2303 |
*/ |
| 2304 |
if (debuginfo || obj_entry_seg == NO_SEG) { |
| 2305 |
orp->type = COMENT; |
| 2306 |
obj_rword(orp, dLINKPASS); |
| 2307 |
obj_byte(orp, 1); |
| 2308 |
obj_emit2(orp); |
| 2309 |
} |
| 2310 |
|
| 2311 |
/* |
| 2312 |
* 1) put out the compiler type |
| 2313 |
* 2) Put out the type info. The only type we are using is near label #19 |
| 2314 |
*/ |
| 2315 |
if (debuginfo) { |
| 2316 |
int i; |
| 2317 |
struct Array *arrtmp = arrhead; |
| 2318 |
orp->type = COMENT; |
| 2319 |
obj_rword(orp, dCOMPDEF); |
| 2320 |
obj_byte(orp, 4); |
| 2321 |
obj_byte(orp, 0); |
| 2322 |
obj_emit2(orp); |
| 2323 |
|
| 2324 |
obj_rword(orp, dTYPEDEF); |
| 2325 |
obj_word(orp, 0x18); /* type # for linking */ |
| 2326 |
obj_word(orp, 6); /* size of type */ |
| 2327 |
obj_byte(orp, 0x2a); /* absolute type for debugging */ |
| 2328 |
obj_emit2(orp); |
| 2329 |
obj_rword(orp, dTYPEDEF); |
| 2330 |
obj_word(orp, 0x19); /* type # for linking */ |
| 2331 |
obj_word(orp, 0); /* size of type */ |
| 2332 |
obj_byte(orp, 0x24); /* absolute type for debugging */ |
| 2333 |
obj_byte(orp, 0); /* near/far specifier */ |
| 2334 |
obj_emit2(orp); |
| 2335 |
obj_rword(orp, dTYPEDEF); |
| 2336 |
obj_word(orp, 0x1A); /* type # for linking */ |
| 2337 |
obj_word(orp, 0); /* size of type */ |
| 2338 |
obj_byte(orp, 0x24); /* absolute type for debugging */ |
| 2339 |
obj_byte(orp, 1); /* near/far specifier */ |
| 2340 |
obj_emit2(orp); |
| 2341 |
obj_rword(orp, dTYPEDEF); |
| 2342 |
obj_word(orp, 0x1b); /* type # for linking */ |
| 2343 |
obj_word(orp, 0); /* size of type */ |
| 2344 |
obj_byte(orp, 0x23); /* absolute type for debugging */ |
| 2345 |
obj_byte(orp, 0); |
| 2346 |
obj_byte(orp, 0); |
| 2347 |
obj_byte(orp, 0); |
| 2348 |
obj_emit2(orp); |
| 2349 |
obj_rword(orp, dTYPEDEF); |
| 2350 |
obj_word(orp, 0x1c); /* type # for linking */ |
| 2351 |
obj_word(orp, 0); /* size of type */ |
| 2352 |
obj_byte(orp, 0x23); /* absolute type for debugging */ |
| 2353 |
obj_byte(orp, 0); |
| 2354 |
obj_byte(orp, 4); |
| 2355 |
obj_byte(orp, 0); |
| 2356 |
obj_emit2(orp); |
| 2357 |
obj_rword(orp, dTYPEDEF); |
| 2358 |
obj_word(orp, 0x1d); /* type # for linking */ |
| 2359 |
obj_word(orp, 0); /* size of type */ |
| 2360 |
obj_byte(orp, 0x23); /* absolute type for debugging */ |
| 2361 |
obj_byte(orp, 0); |
| 2362 |
obj_byte(orp, 1); |
| 2363 |
obj_byte(orp, 0); |
| 2364 |
obj_emit2(orp); |
| 2365 |
obj_rword(orp, dTYPEDEF); |
| 2366 |
obj_word(orp, 0x1e); /* type # for linking */ |
| 2367 |
obj_word(orp, 0); /* size of type */ |
| 2368 |
obj_byte(orp, 0x23); /* absolute type for debugging */ |
| 2369 |
obj_byte(orp, 0); |
| 2370 |
obj_byte(orp, 5); |
| 2371 |
obj_byte(orp, 0); |
| 2372 |
obj_emit2(orp); |
| 2373 |
|
| 2374 |
/* put out the array types */ |
| 2375 |
for (i = ARRAYBOT; i < arrindex; i++) { |
| 2376 |
obj_rword(orp, dTYPEDEF); |
| 2377 |
obj_word(orp, i); /* type # for linking */ |
| 2378 |
obj_word(orp, arrtmp->size); /* size of type */ |
| 2379 |
obj_byte(orp, 0x1A); /* absolute type for debugging (array) */ |
| 2380 |
obj_byte(orp, arrtmp->basetype); /* base type */ |
| 2381 |
obj_emit2(orp); |
| 2382 |
arrtmp = arrtmp->next; |
| 2383 |
} |
| 2384 |
} |
| 2385 |
/* |
| 2386 |
* write out line number info with a LINNUM record |
| 2387 |
* switch records when we switch segments, and output the |
| 2388 |
* file in a pseudo-TASM fashion. The record switch is naive; that |
| 2389 |
* is that one file may have many records for the same segment |
| 2390 |
* if there are lots of segment switches |
| 2391 |
*/ |
| 2392 |
if (fnhead && debuginfo) { |
| 2393 |
seg = fnhead->lnhead->segment; |
| 2394 |
|
| 2395 |
for (fn = fnhead; fn; fn = fn->next) { |
| 2396 |
/* write out current file name */ |
| 2397 |
orp->type = COMENT; |
| 2398 |
orp->ori = ori_null; |
| 2399 |
obj_rword(orp, dFILNAME); |
| 2400 |
obj_byte(orp, 0); |
| 2401 |
obj_name(orp, fn->name); |
| 2402 |
obj_dword(orp, 0); |
| 2403 |
obj_emit2(orp); |
| 2404 |
|
| 2405 |
/* write out line numbers this file */ |
| 2406 |
|
| 2407 |
orp->type = LINNUM; |
| 2408 |
orp->ori = ori_linnum; |
| 2409 |
for (ln = fn->lnhead; ln; ln = ln->next) { |
| 2410 |
if (seg != ln->segment) { |
| 2411 |
/* if we get here have to flush the buffer and start |
| 2412 |
* a new record for a new segment |
| 2413 |
*/ |
| 2414 |
seg = ln->segment; |
| 2415 |
obj_emit(orp); |
| 2416 |
} |
| 2417 |
orp->parm[0] = seg->grp ? seg->grp->obj_index : 0; |
| 2418 |
orp->parm[1] = seg->obj_index; |
| 2419 |
orp = obj_word(orp, ln->lineno); |
| 2420 |
orp = obj_x(orp, ln->offset); |
| 2421 |
obj_commit(orp); |
| 2422 |
} |
| 2423 |
obj_emit(orp); |
| 2424 |
} |
| 2425 |
} |
| 2426 |
/* |
| 2427 |
* we are going to locate the entry point segment now |
| 2428 |
* rather than wait until the MODEND record, because, |
| 2429 |
* then we can output a special symbol to tell where the |
| 2430 |
* entry point is. |
| 2431 |
* |
| 2432 |
*/ |
| 2433 |
if (obj_entry_seg != NO_SEG) { |
| 2434 |
for (seg = seghead; seg; seg = seg->next) { |
| 2435 |
if (seg->index == obj_entry_seg) { |
| 2436 |
entry_seg_ptr = seg; |
| 2437 |
break; |
| 2438 |
} |
| 2439 |
} |
| 2440 |
if (!seg) |
| 2441 |
nasm_nonfatal("entry point is not in this module"); |
| 2442 |
} |
| 2443 |
|
| 2444 |
/* |
| 2445 |
* get ready to put out symbol records |
| 2446 |
*/ |
| 2447 |
orp->type = COMENT; |
| 2448 |
orp->ori = ori_local; |
| 2449 |
|
| 2450 |
/* |
| 2451 |
* put out a symbol for the entry point |
| 2452 |
* no dots in this symbol, because, borland does |
| 2453 |
* not (officially) support dots in label names |
| 2454 |
* and I don't know what various versions of TLINK will do |
| 2455 |
*/ |
| 2456 |
if (debuginfo && obj_entry_seg != NO_SEG) { |
| 2457 |
orp = obj_name(orp, "start_of_program"); |
| 2458 |
orp = obj_word(orp, 0x19); /* type: near label */ |
| 2459 |
orp = obj_index(orp, seg->grp ? seg->grp->obj_index : 0); |
| 2460 |
orp = obj_index(orp, seg->obj_index); |
| 2461 |
orp = obj_x(orp, obj_entry_ofs); |
| 2462 |
obj_commit(orp); |
| 2463 |
} |
| 2464 |
|
| 2465 |
/* |
| 2466 |
* put out the local labels |
| 2467 |
*/ |
| 2468 |
for (seg = seghead; seg && debuginfo; seg = seg->next) { |
| 2469 |
/* labels this seg */ |
| 2470 |
for (loc = seg->lochead; loc; loc = loc->next) { |
| 2471 |
orp = obj_name(orp, loc->name); |
| 2472 |
orp = obj_word(orp, loc->type); |
| 2473 |
orp = obj_index(orp, seg->grp ? seg->grp->obj_index : 0); |
| 2474 |
orp = obj_index(orp, seg->obj_index); |
| 2475 |
orp = obj_x(orp, loc->offset); |
| 2476 |
obj_commit(orp); |
| 2477 |
} |
| 2478 |
} |
| 2479 |
if (orp->used) |
| 2480 |
obj_emit(orp); |
| 2481 |
|
| 2482 |
/* |
| 2483 |
* Write the LEDATA/FIXUPP pairs. |
| 2484 |
*/ |
| 2485 |
for (seg = seghead; seg; seg = seg->next) { |
| 2486 |
obj_emit(seg->orp); |
| 2487 |
nasm_free(seg->orp); |
| 2488 |
} |
| 2489 |
|
| 2490 |
/* |
| 2491 |
* Write the MODEND module end marker. |
| 2492 |
*/ |
| 2493 |
orp->type = obj_use32 ? MODE32 : MODEND; |
| 2494 |
orp->ori = ori_null; |
| 2495 |
if (entry_seg_ptr) { |
| 2496 |
orp->type = entry_seg_ptr->use32 ? MODE32 : MODEND; |
| 2497 |
obj_byte(orp, 0xC1); |
| 2498 |
seg = entry_seg_ptr; |
| 2499 |
if (seg->grp) { |
| 2500 |
obj_byte(orp, 0x10); |
| 2501 |
obj_index(orp, seg->grp->obj_index); |
| 2502 |
} else { |
| 2503 |
/* |
| 2504 |
* the below changed to prevent TLINK crashing. |
| 2505 |
* Previous more efficient version read: |
| 2506 |
* |
| 2507 |
* obj_byte (orp, 0x50); |
| 2508 |
*/ |
| 2509 |
obj_byte(orp, 0x00); |
| 2510 |
obj_index(orp, seg->obj_index); |
| 2511 |
} |
| 2512 |
obj_index(orp, seg->obj_index); |
| 2513 |
obj_x(orp, obj_entry_ofs); |
| 2514 |
} else |
| 2515 |
obj_byte(orp, 0); |
| 2516 |
obj_emit2(orp); |
| 2517 |
nasm_free(orp); |
| 2518 |
} |
| 2519 |
|
| 2520 |
static void obj_fwrite(ObjRecord * orp) |
| 2521 |
{ |
| 2522 |
unsigned int cksum, len; |
| 2523 |
uint8_t *ptr; |
| 2524 |
|
| 2525 |
cksum = orp->type; |
| 2526 |
if (orp->x_size == 32) |
| 2527 |
cksum |= 1; |
| 2528 |
fputc(cksum, ofile); |
| 2529 |
len = orp->committed + 1; |
| 2530 |
cksum += (len & 0xFF) + ((len >> 8) & 0xFF); |
| 2531 |
fwriteint16_t(len, ofile); |
| 2532 |
nasm_write(orp->buf, len-1, ofile); |
| 2533 |
for (ptr = orp->buf; --len; ptr++) |
| 2534 |
cksum += *ptr; |
| 2535 |
fputc((-cksum) & 0xFF, ofile); |
| 2536 |
} |
| 2537 |
|
| 2538 |
static enum directive_result |
| 2539 |
obj_pragma(const struct pragma *pragma) |
| 2540 |
{ |
| 2541 |
switch (pragma->opcode) { |
| 2542 |
case D_NODEPEND: |
| 2543 |
obj_nodepend = true; |
| 2544 |
break; |
| 2545 |
|
| 2546 |
default: |
| 2547 |
break; |
| 2548 |
} |
| 2549 |
|
| 2550 |
return DIRR_OK; |
| 2551 |
} |
| 2552 |
|
| 2553 |
extern macros_t obj_stdmac[]; |
| 2554 |
|
| 2555 |
static void dbgbi_init(void) |
| 2556 |
{ |
| 2557 |
fnhead = NULL; |
| 2558 |
fntail = &fnhead; |
| 2559 |
arrindex = ARRAYBOT; |
| 2560 |
arrhead = NULL; |
| 2561 |
arrtail = &arrhead; |
| 2562 |
} |
| 2563 |
static void dbgbi_cleanup(void) |
| 2564 |
{ |
| 2565 |
struct Segment *segtmp; |
| 2566 |
while (fnhead) { |
| 2567 |
struct FileName *fntemp = fnhead; |
| 2568 |
while (fnhead->lnhead) { |
| 2569 |
struct LineNumber *lntemp = fnhead->lnhead; |
| 2570 |
fnhead->lnhead = lntemp->next; |
| 2571 |
nasm_free(lntemp); |
| 2572 |
} |
| 2573 |
fnhead = fnhead->next; |
| 2574 |
nasm_free(fntemp->name); |
| 2575 |
nasm_free(fntemp); |
| 2576 |
} |
| 2577 |
for (segtmp = seghead; segtmp; segtmp = segtmp->next) { |
| 2578 |
while (segtmp->lochead) { |
| 2579 |
struct Public *loctmp = segtmp->lochead; |
| 2580 |
segtmp->lochead = loctmp->next; |
| 2581 |
nasm_free(loctmp->name); |
| 2582 |
nasm_free(loctmp); |
| 2583 |
} |
| 2584 |
} |
| 2585 |
while (arrhead) { |
| 2586 |
struct Array *arrtmp = arrhead; |
| 2587 |
arrhead = arrhead->next; |
| 2588 |
nasm_free(arrtmp); |
| 2589 |
} |
| 2590 |
} |
| 2591 |
|
| 2592 |
static void dbgbi_linnum(const char *lnfname, int32_t lineno, int32_t segto) |
| 2593 |
{ |
| 2594 |
struct FileName *fn; |
| 2595 |
struct LineNumber *ln; |
| 2596 |
struct Segment *seg; |
| 2597 |
|
| 2598 |
if (segto == NO_SEG) |
| 2599 |
return; |
| 2600 |
|
| 2601 |
/* |
| 2602 |
* If `any_segs' is still false, we must define a default |
| 2603 |
* segment. |
| 2604 |
*/ |
| 2605 |
if (!any_segs) { |
| 2606 |
int tempint = 0; |
| 2607 |
if (segto != obj_segment(DEFAULT_SEG, &tempint)) |
| 2608 |
nasm_panic("strange segment conditions in OBJ driver"); |
| 2609 |
} |
| 2610 |
|
| 2611 |
/* |
| 2612 |
* Find the segment we are targeting. |
| 2613 |
*/ |
| 2614 |
for (seg = seghead; seg; seg = seg->next) |
| 2615 |
if (seg->index == segto) |
| 2616 |
break; |
| 2617 |
if (!seg) |
| 2618 |
nasm_panic("lineno directed to nonexistent segment?"); |
| 2619 |
|
| 2620 |
/* for (fn = fnhead; fn; fn = fnhead->next) */ |
| 2621 |
for (fn = fnhead; fn; fn = fn->next) /* fbk - Austin Lunnen - John Fine */ |
| 2622 |
if (!nasm_stricmp(lnfname, fn->name)) |
| 2623 |
break; |
| 2624 |
if (!fn) { |
| 2625 |
fn = nasm_malloc(sizeof(*fn)); |
| 2626 |
fn->name = nasm_malloc(strlen(lnfname) + 1); |
| 2627 |
strcpy(fn->name, lnfname); |
| 2628 |
fn->lnhead = NULL; |
| 2629 |
fn->lntail = &fn->lnhead; |
| 2630 |
fn->next = NULL; |
| 2631 |
*fntail = fn; |
| 2632 |
fntail = &fn->next; |
| 2633 |
} |
| 2634 |
ln = nasm_malloc(sizeof(*ln)); |
| 2635 |
ln->segment = seg; |
| 2636 |
ln->offset = seg->currentpos; |
| 2637 |
ln->lineno = lineno; |
| 2638 |
ln->next = NULL; |
| 2639 |
*fn->lntail = ln; |
| 2640 |
fn->lntail = &ln->next; |
| 2641 |
|
| 2642 |
} |
| 2643 |
static void dbgbi_deflabel(char *name, int32_t segment, |
| 2644 |
int64_t offset, int is_global, char *special) |
| 2645 |
{ |
| 2646 |
struct Segment *seg; |
| 2647 |
|
| 2648 |
(void)special; |
| 2649 |
|
| 2650 |
/* |
| 2651 |
* Note: ..[^@] special symbols are filtered in labels.c |
| 2652 |
*/ |
| 2653 |
|
| 2654 |
/* |
| 2655 |
* If it's a special-retry from pass two, discard it. |
| 2656 |
*/ |
| 2657 |
if (is_global == 3) |
| 2658 |
return; |
| 2659 |
|
| 2660 |
/* |
| 2661 |
* Case (i): |
| 2662 |
*/ |
| 2663 |
if (obj_seg_needs_update) { |
| 2664 |
return; |
| 2665 |
} else if (obj_grp_needs_update) { |
| 2666 |
return; |
| 2667 |
} |
| 2668 |
if (segment < SEG_ABS && segment != NO_SEG && segment % 2) |
| 2669 |
return; |
| 2670 |
|
| 2671 |
if (segment >= SEG_ABS || segment == NO_SEG) { |
| 2672 |
return; |
| 2673 |
} |
| 2674 |
|
| 2675 |
/* |
| 2676 |
* If `any_segs' is still false, we might need to define a |
| 2677 |
* default segment, if they're trying to declare a label in |
| 2678 |
* `first_seg'. But the label should exist due to a prior |
| 2679 |
* call to obj_deflabel so we can skip that. |
| 2680 |
*/ |
| 2681 |
|
| 2682 |
for (seg = seghead; seg; seg = seg->next) |
| 2683 |
if (seg->index == segment) { |
| 2684 |
struct Public *loc = nasm_malloc(sizeof(*loc)); |
| 2685 |
/* |
| 2686 |
* Case (ii). Maybe MODPUB someday? |
| 2687 |
*/ |
| 2688 |
last_defined = *seg->loctail = loc; |
| 2689 |
seg->loctail = &loc->next; |
| 2690 |
loc->next = NULL; |
| 2691 |
loc->name = nasm_strdup(name); |
| 2692 |
loc->offset = offset; |
| 2693 |
} |
| 2694 |
} |
| 2695 |
static void dbgbi_typevalue(int32_t type) |
| 2696 |
{ |
| 2697 |
int vsize; |
| 2698 |
int elem = TYM_ELEMENTS(type); |
| 2699 |
type = TYM_TYPE(type); |
| 2700 |
|
| 2701 |
if (!last_defined) |
| 2702 |
return; |
| 2703 |
|
| 2704 |
switch (type) { |
| 2705 |
case TY_BYTE: |
| 2706 |
last_defined->type = 8; /* uint8_t */ |
| 2707 |
vsize = 1; |
| 2708 |
break; |
| 2709 |
case TY_WORD: |
| 2710 |
last_defined->type = 10; /* unsigned word */ |
| 2711 |
vsize = 2; |
| 2712 |
break; |
| 2713 |
case TY_DWORD: |
| 2714 |
last_defined->type = 12; /* unsigned dword */ |
| 2715 |
vsize = 4; |
| 2716 |
break; |
| 2717 |
case TY_FLOAT: |
| 2718 |
last_defined->type = 14; /* float */ |
| 2719 |
vsize = 4; |
| 2720 |
break; |
| 2721 |
case TY_QWORD: |
| 2722 |
last_defined->type = 15; /* qword */ |
| 2723 |
vsize = 8; |
| 2724 |
break; |
| 2725 |
case TY_TBYTE: |
| 2726 |
last_defined->type = 16; /* TBYTE */ |
| 2727 |
vsize = 10; |
| 2728 |
break; |
| 2729 |
default: |
| 2730 |
last_defined->type = 0x19; /* label */ |
| 2731 |
vsize = 0; |
| 2732 |
break; |
| 2733 |
} |
| 2734 |
|
| 2735 |
if (elem > 1) { |
| 2736 |
struct Array *arrtmp = nasm_malloc(sizeof(*arrtmp)); |
| 2737 |
int vtype = last_defined->type; |
| 2738 |
arrtmp->size = vsize * elem; |
| 2739 |
arrtmp->basetype = vtype; |
| 2740 |
arrtmp->next = NULL; |
| 2741 |
last_defined->type = arrindex++; |
| 2742 |
*arrtail = arrtmp; |
| 2743 |
arrtail = &(arrtmp->next); |
| 2744 |
} |
| 2745 |
last_defined = NULL; |
| 2746 |
} |
| 2747 |
static void dbgbi_output(int output_type, void *param) |
| 2748 |
{ |
| 2749 |
(void)output_type; |
| 2750 |
(void)param; |
| 2751 |
} |
| 2752 |
static const struct dfmt borland_debug_form = { |
| 2753 |
"Borland Debug Records", |
| 2754 |
"borland", |
| 2755 |
dbgbi_init, |
| 2756 |
dbgbi_linnum, |
| 2757 |
dbgbi_deflabel, |
| 2758 |
NULL, /* .debug_smacros */ |
| 2759 |
NULL, /* .debug_include */ |
| 2760 |
NULL, /* .debug_mmacros */ |
| 2761 |
null_debug_directive, |
| 2762 |
dbgbi_typevalue, |
| 2763 |
dbgbi_output, |
| 2764 |
dbgbi_cleanup, |
| 2765 |
NULL /* pragma list */ |
| 2766 |
}; |
| 2767 |
|
| 2768 |
static const struct dfmt * const borland_debug_arr[3] = { |
| 2769 |
&borland_debug_form, |
| 2770 |
&null_debug_form, |
| 2771 |
NULL |
| 2772 |
}; |
| 2773 |
|
| 2774 |
static const struct pragma_facility obj_pragma_list[] = { |
| 2775 |
{ NULL, obj_pragma } |
| 2776 |
}; |
| 2777 |
|
| 2778 |
const struct ofmt of_obj = { |
| 2779 |
"Intel/Microsoft OMF (MS-DOS, OS/2, Win16)", |
| 2780 |
"obj", |
| 2781 |
".obj", |
| 2782 |
0, |
| 2783 |
32, |
| 2784 |
borland_debug_arr, |
| 2785 |
&borland_debug_form, |
| 2786 |
obj_stdmac, |
| 2787 |
obj_init, |
| 2788 |
null_reset, |
| 2789 |
obj_out, |
| 2790 |
obj_deflabel, |
| 2791 |
obj_segment, |
| 2792 |
NULL, |
| 2793 |
obj_sectalign, |
| 2794 |
obj_segbase, |
| 2795 |
obj_directive, |
| 2796 |
obj_cleanup, |
| 2797 |
obj_pragma_list |
| 2798 |
}; |
| 2799 |
|
| 2800 |
const struct ofmt of_obj2 = { |
| 2801 |
"Intel/Microsoft OMF (i386) (OS/2)", |
| 2802 |
"obj2", |
| 2803 |
".obj", |
| 2804 |
0, |
| 2805 |
32, |
| 2806 |
borland_debug_arr, |
| 2807 |
&borland_debug_form, |
| 2808 |
obj_stdmac, |
| 2809 |
obj_init, |
| 2810 |
null_reset, |
| 2811 |
obj_out, |
| 2812 |
obj_deflabel, |
| 2813 |
obj_segment, |
| 2814 |
NULL, |
| 2815 |
obj_sectalign, |
| 2816 |
obj_segbase, |
| 2817 |
obj_directive, |
| 2818 |
obj_cleanup, |
| 2819 |
obj_pragma_list |
| 2820 |
}; |
| 2821 |
#endif /* OF_OBJ || OF_OBJ2 */ |