outieee.c 44 KB

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