outobj.c 81 KB

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