bin-objfmt.c 64 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972
  1. /*
  2. * Flat-format binary object format
  3. *
  4. * Copyright (C) 2002-2007 Peter Johnson
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND OTHER CONTRIBUTORS ``AS IS''
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  18. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR OTHER CONTRIBUTORS BE
  19. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  20. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  21. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  22. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  23. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  24. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  25. * POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #include <util.h>
  28. #ifdef HAVE_UNISTD_H
  29. #include <unistd.h>
  30. #endif
  31. #include <libyasm.h>
  32. #define REGULAR_OUTBUF_SIZE 1024
  33. typedef struct bin_section_data {
  34. int bss; /* aka nobits */
  35. /* User-provided alignment */
  36. yasm_intnum *align, *valign;
  37. /* User-provided starts */
  38. /*@null@*/ /*@owned@*/ yasm_expr *start, *vstart;
  39. /* User-provided follows */
  40. /*@null@*/ /*@owned@*/ char *follows, *vfollows;
  41. /* Calculated (final) starts, used only during output() */
  42. /*@null@*/ /*@owned@*/ yasm_intnum *istart, *ivstart;
  43. /* Calculated (final) length, used only during output() */
  44. /*@null@*/ /*@owned@*/ yasm_intnum *length;
  45. } bin_section_data;
  46. typedef struct yasm_objfmt_bin {
  47. yasm_objfmt_base objfmt; /* base structure */
  48. enum {
  49. NO_MAP = 0,
  50. MAP_NONE = 0x01,
  51. MAP_BRIEF = 0x02,
  52. MAP_SECTIONS = 0x04,
  53. MAP_SYMBOLS = 0x08
  54. } map_flags;
  55. /*@null@*/ /*@only@*/ char *map_filename;
  56. /*@null@*/ /*@only@*/ yasm_expr *org;
  57. } yasm_objfmt_bin;
  58. /* symrec data is used only for the special symbols section<sectname>.start,
  59. * section<sectname>.vstart, and section<sectname>.length
  60. */
  61. typedef struct bin_symrec_data {
  62. yasm_section *section; /* referenced section */
  63. enum bin_ssym {
  64. SSYM_START,
  65. SSYM_VSTART,
  66. SSYM_LENGTH
  67. } which;
  68. } bin_symrec_data;
  69. static void bin_section_data_destroy(/*@only@*/ void *d);
  70. static void bin_section_data_print(void *data, FILE *f, int indent_level);
  71. static const yasm_assoc_data_callback bin_section_data_cb = {
  72. bin_section_data_destroy,
  73. bin_section_data_print
  74. };
  75. static void bin_symrec_data_destroy(/*@only@*/ void *d);
  76. static void bin_symrec_data_print(void *data, FILE *f, int indent_level);
  77. static const yasm_assoc_data_callback bin_symrec_data_cb = {
  78. bin_symrec_data_destroy,
  79. bin_symrec_data_print
  80. };
  81. yasm_objfmt_module yasm_bin_LTX_objfmt;
  82. static yasm_objfmt *
  83. bin_objfmt_create(yasm_object *object)
  84. {
  85. yasm_objfmt_bin *objfmt_bin = yasm_xmalloc(sizeof(yasm_objfmt_bin));
  86. objfmt_bin->objfmt.module = &yasm_bin_LTX_objfmt;
  87. objfmt_bin->map_flags = NO_MAP;
  88. objfmt_bin->map_filename = NULL;
  89. objfmt_bin->org = NULL;
  90. return (yasm_objfmt *)objfmt_bin;
  91. }
  92. typedef TAILQ_HEAD(bin_group_head, bin_group) bin_groups;
  93. typedef struct bin_group {
  94. TAILQ_ENTRY(bin_group) link;
  95. yasm_section *section;
  96. bin_section_data *bsd;
  97. /* Groups that (in parallel) logically come immediately after this
  98. * group's section.
  99. */
  100. bin_groups follow_groups;
  101. } bin_group;
  102. /* Recursive function to find group containing named section. */
  103. static bin_group *
  104. find_group_by_name(bin_groups *groups, const char *name)
  105. {
  106. bin_group *group, *found;
  107. TAILQ_FOREACH(group, groups, link) {
  108. if (strcmp(yasm_section_get_name(group->section), name) == 0)
  109. return group;
  110. /* Recurse to loop through follow groups */
  111. found = find_group_by_name(&group->follow_groups, name);
  112. if (found)
  113. return found;
  114. }
  115. return NULL;
  116. }
  117. /* Recursive function to find group. Returns NULL if not found. */
  118. static bin_group *
  119. find_group_by_section(bin_groups *groups, yasm_section *section)
  120. {
  121. bin_group *group, *found;
  122. TAILQ_FOREACH(group, groups, link) {
  123. if (group->section == section)
  124. return group;
  125. /* Recurse to loop through follow groups */
  126. found = find_group_by_section(&group->follow_groups, section);
  127. if (found)
  128. return found;
  129. }
  130. return NULL;
  131. }
  132. #if 0
  133. /* Debugging function */
  134. static void
  135. print_groups(const bin_groups *groups, int indent_level)
  136. {
  137. bin_group *group;
  138. TAILQ_FOREACH(group, groups, link) {
  139. printf("%*sSection `%s':\n", indent_level, "",
  140. yasm_section_get_name(group->section));
  141. bin_section_data_print(group->bsd, stdout, indent_level+1);
  142. if (!TAILQ_EMPTY(&group->follow_groups)) {
  143. printf("%*sFollowing groups:\n", indent_level, "");
  144. print_groups(&group->follow_groups, indent_level+1);
  145. }
  146. }
  147. }
  148. #endif
  149. static void
  150. bin_group_destroy(/*@only@*/ bin_group *group)
  151. {
  152. bin_group *follow, *group_temp;
  153. TAILQ_FOREACH_SAFE(follow, &group->follow_groups, link, group_temp)
  154. bin_group_destroy(follow);
  155. yasm_xfree(group);
  156. }
  157. typedef struct bin_objfmt_output_info {
  158. yasm_object *object;
  159. yasm_errwarns *errwarns;
  160. /*@dependent@*/ FILE *f;
  161. /*@only@*/ unsigned char *buf;
  162. /*@observer@*/ const yasm_section *sect;
  163. unsigned long start; /* what normal variables go against */
  164. yasm_intnum *origin;
  165. yasm_intnum *tmp_intn; /* temporary working intnum */
  166. bin_groups lma_groups, vma_groups;
  167. } bin_objfmt_output_info;
  168. static int
  169. bin_objfmt_check_sym(yasm_symrec *sym, /*@null@*/ void *d)
  170. {
  171. /*@null@*/ bin_objfmt_output_info *info = (bin_objfmt_output_info *)d;
  172. yasm_sym_vis vis = yasm_symrec_get_visibility(sym);
  173. assert(info != NULL);
  174. /* Don't check internally-generated symbols. Only internally generated
  175. * symbols have symrec data, so simply check for its presence.
  176. */
  177. if (yasm_symrec_get_data(sym, &bin_symrec_data_cb))
  178. return 0;
  179. if (vis & YASM_SYM_EXTERN) {
  180. yasm_warn_set(YASM_WARN_GENERAL,
  181. N_("binary object format does not support extern variables"));
  182. yasm_errwarn_propagate(info->errwarns, yasm_symrec_get_decl_line(sym));
  183. } else if (vis & YASM_SYM_GLOBAL) {
  184. yasm_warn_set(YASM_WARN_GENERAL,
  185. N_("binary object format does not support global variables"));
  186. yasm_errwarn_propagate(info->errwarns, yasm_symrec_get_decl_line(sym));
  187. } else if (vis & YASM_SYM_COMMON) {
  188. yasm_error_set(YASM_ERROR_TYPE,
  189. N_("binary object format does not support common variables"));
  190. yasm_errwarn_propagate(info->errwarns, yasm_symrec_get_decl_line(sym));
  191. }
  192. return 0;
  193. }
  194. static int
  195. bin_lma_create_group(yasm_section *sect, /*@null@*/ void *d)
  196. {
  197. bin_objfmt_output_info *info = (bin_objfmt_output_info *)d;
  198. bin_section_data *bsd = yasm_section_get_data(sect, &bin_section_data_cb);
  199. unsigned long align = yasm_section_get_align(sect);
  200. bin_group *group;
  201. assert(info != NULL);
  202. assert(bsd != NULL);
  203. group = yasm_xmalloc(sizeof(bin_group));
  204. group->section = sect;
  205. group->bsd = bsd;
  206. TAILQ_INIT(&group->follow_groups);
  207. /* Determine section alignment as necessary. */
  208. if (!bsd->align)
  209. bsd->align = yasm_intnum_create_uint(align > 4 ? align : 4);
  210. else {
  211. yasm_intnum *align_intn = yasm_intnum_create_uint(align);
  212. if (yasm_intnum_compare(align_intn, bsd->align) > 0) {
  213. yasm_warn_set(YASM_WARN_GENERAL,
  214. N_("section `%s' internal align of %lu is greater than `%s' of %lu; using `%s'"),
  215. yasm_section_get_name(sect),
  216. yasm_intnum_get_uint(align_intn),
  217. N_("align"),
  218. yasm_intnum_get_uint(bsd->align),
  219. N_("align"));
  220. yasm_errwarn_propagate(info->errwarns, 0);
  221. }
  222. yasm_intnum_destroy(align_intn);
  223. }
  224. /* Calculate section integer start. */
  225. if (bsd->start) {
  226. bsd->istart = yasm_expr_get_intnum(&bsd->start, 0);
  227. if (!bsd->istart) {
  228. yasm_error_set(YASM_ERROR_TOO_COMPLEX,
  229. N_("start expression is too complex"));
  230. yasm_errwarn_propagate(info->errwarns, bsd->start->line);
  231. return 1;
  232. } else
  233. bsd->istart = yasm_intnum_copy(bsd->istart);
  234. } else
  235. bsd->istart = NULL;
  236. /* Calculate section integer vstart. */
  237. if (bsd->vstart) {
  238. bsd->ivstart = yasm_expr_get_intnum(&bsd->vstart, 0);
  239. if (!bsd->ivstart) {
  240. yasm_error_set(YASM_ERROR_TOO_COMPLEX,
  241. N_("vstart expression is too complex"));
  242. yasm_errwarn_propagate(info->errwarns, bsd->vstart->line);
  243. return 1;
  244. } else
  245. bsd->ivstart = yasm_intnum_copy(bsd->ivstart);
  246. } else
  247. bsd->ivstart = NULL;
  248. /* Calculate section integer length. */
  249. bsd->length = yasm_calc_bc_dist(yasm_section_bcs_first(sect),
  250. yasm_section_bcs_last(sect));
  251. TAILQ_INSERT_TAIL(&info->lma_groups, group, link);
  252. return 0;
  253. }
  254. static int
  255. bin_vma_create_group(yasm_section *sect, /*@null@*/ void *d)
  256. {
  257. bin_objfmt_output_info *info = (bin_objfmt_output_info *)d;
  258. bin_section_data *bsd = yasm_section_get_data(sect, &bin_section_data_cb);
  259. bin_group *group;
  260. assert(info != NULL);
  261. assert(bsd != NULL);
  262. group = yasm_xmalloc(sizeof(bin_group));
  263. group->section = sect;
  264. group->bsd = bsd;
  265. TAILQ_INIT(&group->follow_groups);
  266. TAILQ_INSERT_TAIL(&info->vma_groups, group, link);
  267. return 0;
  268. }
  269. /* Calculates new start address based on alignment constraint.
  270. * Start is modified (rounded up) to the closest aligned value greater than
  271. * what was passed in.
  272. * Align must be a power of 2.
  273. */
  274. static void
  275. bin_objfmt_align(yasm_intnum *start, const yasm_intnum *align)
  276. {
  277. /* Because alignment is always a power of two, we can use some bit
  278. * trickery to do this easily.
  279. */
  280. yasm_intnum *align_intn =
  281. yasm_intnum_create_uint(yasm_intnum_get_uint(align)-1);
  282. yasm_intnum_calc(align_intn, YASM_EXPR_AND, start);
  283. if (!yasm_intnum_is_zero(align_intn)) {
  284. /* start = (start & ~(align-1)) + align; */
  285. yasm_intnum_set_uint(align_intn, yasm_intnum_get_uint(align)-1);
  286. yasm_intnum_calc(align_intn, YASM_EXPR_NOT, NULL);
  287. yasm_intnum_calc(align_intn, YASM_EXPR_AND, start);
  288. yasm_intnum_set(start, align);
  289. yasm_intnum_calc(start, YASM_EXPR_ADD, align_intn);
  290. }
  291. yasm_intnum_destroy(align_intn);
  292. }
  293. /* Recursive function to assign start addresses.
  294. * Updates start, last, and vdelta parameters as it goes along.
  295. * The tmp parameter is just a working intnum so one doesn't have to be
  296. * locally allocated for this purpose.
  297. */
  298. static void
  299. group_assign_start_recurse(bin_group *group, yasm_intnum *start,
  300. yasm_intnum *last, yasm_intnum *vdelta,
  301. yasm_intnum *tmp, yasm_errwarns *errwarns)
  302. {
  303. bin_group *follow_group;
  304. /* Determine LMA */
  305. if (group->bsd->istart) {
  306. yasm_intnum_set(group->bsd->istart, start);
  307. if (group->bsd->align) {
  308. bin_objfmt_align(group->bsd->istart, group->bsd->align);
  309. if (yasm_intnum_compare(start, group->bsd->istart) != 0) {
  310. yasm_warn_set(YASM_WARN_GENERAL,
  311. N_("start inconsistent with align; using aligned value"));
  312. yasm_errwarn_propagate(errwarns, group->bsd->start->line);
  313. }
  314. }
  315. } else {
  316. group->bsd->istart = yasm_intnum_copy(start);
  317. if (group->bsd->align != 0)
  318. bin_objfmt_align(group->bsd->istart, group->bsd->align);
  319. }
  320. /* Determine VMA if either just valign specified or if no v* specified */
  321. if (!group->bsd->vstart) {
  322. if (!group->bsd->vfollows && !group->bsd->valign) {
  323. /* No v* specified, set VMA=LMA+vdelta. */
  324. group->bsd->ivstart = yasm_intnum_copy(group->bsd->istart);
  325. yasm_intnum_calc(group->bsd->ivstart, YASM_EXPR_ADD, vdelta);
  326. } else if (!group->bsd->vfollows) {
  327. /* Just valign specified: set VMA=LMA+vdelta, align VMA, then add
  328. * delta between unaligned and aligned to vdelta parameter.
  329. */
  330. group->bsd->ivstart = yasm_intnum_copy(group->bsd->istart);
  331. yasm_intnum_calc(group->bsd->ivstart, YASM_EXPR_ADD, vdelta);
  332. yasm_intnum_set(tmp, group->bsd->ivstart);
  333. bin_objfmt_align(group->bsd->ivstart, group->bsd->valign);
  334. yasm_intnum_calc(vdelta, YASM_EXPR_ADD, group->bsd->ivstart);
  335. yasm_intnum_calc(vdelta, YASM_EXPR_SUB, tmp);
  336. }
  337. }
  338. /* Find the maximum end value */
  339. yasm_intnum_set(tmp, group->bsd->istart);
  340. yasm_intnum_calc(tmp, YASM_EXPR_ADD, group->bsd->length);
  341. if (yasm_intnum_compare(tmp, last) > 0) /* tmp > last */
  342. yasm_intnum_set(last, tmp);
  343. /* Recurse for each following group. */
  344. TAILQ_FOREACH(follow_group, &group->follow_groups, link) {
  345. /* Following sections have to follow this one,
  346. * so add length to start.
  347. */
  348. yasm_intnum_set(start, group->bsd->istart);
  349. yasm_intnum_calc(start, YASM_EXPR_ADD, group->bsd->length);
  350. group_assign_start_recurse(follow_group, start, last, vdelta, tmp,
  351. errwarns);
  352. }
  353. }
  354. /* Recursive function to assign start addresses.
  355. * Updates start parameter as it goes along.
  356. * The tmp parameter is just a working intnum so one doesn't have to be
  357. * locally allocated for this purpose.
  358. */
  359. static void
  360. group_assign_vstart_recurse(bin_group *group, yasm_intnum *start,
  361. yasm_errwarns *errwarns)
  362. {
  363. bin_group *follow_group;
  364. /* Determine VMA section alignment as necessary.
  365. * Default to LMA alignment if not specified.
  366. */
  367. if (!group->bsd->valign)
  368. group->bsd->valign = yasm_intnum_copy(group->bsd->align);
  369. else {
  370. unsigned long align = yasm_section_get_align(group->section);
  371. yasm_intnum *align_intn = yasm_intnum_create_uint(align);
  372. if (yasm_intnum_compare(align_intn, group->bsd->valign) > 0) {
  373. yasm_warn_set(YASM_WARN_GENERAL,
  374. N_("section `%s' internal align of %lu is greater than `%s' of %lu; using `%s'"),
  375. yasm_section_get_name(group->section),
  376. yasm_intnum_get_uint(align_intn),
  377. N_("valign"),
  378. yasm_intnum_get_uint(group->bsd->valign),
  379. N_("valign"));
  380. yasm_errwarn_propagate(errwarns, 0);
  381. }
  382. yasm_intnum_destroy(align_intn);
  383. }
  384. /* Determine VMA */
  385. if (group->bsd->ivstart) {
  386. yasm_intnum_set(group->bsd->ivstart, start);
  387. if (group->bsd->valign) {
  388. bin_objfmt_align(group->bsd->ivstart, group->bsd->valign);
  389. if (yasm_intnum_compare(start, group->bsd->ivstart) != 0) {
  390. yasm_error_set(YASM_ERROR_VALUE,
  391. N_("vstart inconsistent with valign"));
  392. yasm_errwarn_propagate(errwarns, group->bsd->vstart->line);
  393. }
  394. }
  395. } else {
  396. group->bsd->ivstart = yasm_intnum_copy(start);
  397. if (group->bsd->valign)
  398. bin_objfmt_align(group->bsd->ivstart, group->bsd->valign);
  399. }
  400. /* Recurse for each following group. */
  401. TAILQ_FOREACH(follow_group, &group->follow_groups, link) {
  402. /* Following sections have to follow this one,
  403. * so add length to start.
  404. */
  405. yasm_intnum_set(start, group->bsd->ivstart);
  406. yasm_intnum_calc(start, YASM_EXPR_ADD, group->bsd->length);
  407. group_assign_vstart_recurse(follow_group, start, errwarns);
  408. }
  409. }
  410. static /*@null@*/ const yasm_intnum *
  411. get_ssym_value(yasm_symrec *sym)
  412. {
  413. bin_symrec_data *bsymd = yasm_symrec_get_data(sym, &bin_symrec_data_cb);
  414. bin_section_data *bsd;
  415. if (!bsymd)
  416. return NULL;
  417. bsd = yasm_section_get_data(bsymd->section, &bin_section_data_cb);
  418. assert(bsd != NULL);
  419. switch (bsymd->which) {
  420. case SSYM_START: return bsd->istart;
  421. case SSYM_VSTART: return bsd->ivstart;
  422. case SSYM_LENGTH: return bsd->length;
  423. }
  424. return NULL;
  425. }
  426. static /*@only@*/ yasm_expr *
  427. bin_objfmt_expr_xform(/*@returned@*/ /*@only@*/ yasm_expr *e,
  428. /*@unused@*/ /*@null@*/ void *d)
  429. {
  430. int i;
  431. for (i=0; i<e->numterms; i++) {
  432. /*@dependent@*/ yasm_section *sect;
  433. /*@dependent@*/ /*@null@*/ yasm_bytecode *precbc;
  434. /*@null@*/ yasm_intnum *dist;
  435. /*@null@*/ const yasm_intnum *ssymval;
  436. /* Transform symrecs or precbcs that reference sections into
  437. * vstart + intnum(dist).
  438. */
  439. if (((e->terms[i].type == YASM_EXPR_SYM &&
  440. yasm_symrec_get_label(e->terms[i].data.sym, &precbc)) ||
  441. (e->terms[i].type == YASM_EXPR_PRECBC &&
  442. (precbc = e->terms[i].data.precbc))) &&
  443. (sect = yasm_bc_get_section(precbc)) &&
  444. (dist = yasm_calc_bc_dist(yasm_section_bcs_first(sect), precbc))) {
  445. bin_section_data *bsd;
  446. bsd = yasm_section_get_data(sect, &bin_section_data_cb);
  447. assert(bsd != NULL);
  448. yasm_intnum_calc(dist, YASM_EXPR_ADD, bsd->ivstart);
  449. e->terms[i].type = YASM_EXPR_INT;
  450. e->terms[i].data.intn = dist;
  451. }
  452. /* Transform our special symrecs into the appropriate value */
  453. if (e->terms[i].type == YASM_EXPR_SYM &&
  454. (ssymval = get_ssym_value(e->terms[i].data.sym))) {
  455. e->terms[i].type = YASM_EXPR_INT;
  456. e->terms[i].data.intn = yasm_intnum_copy(ssymval);
  457. }
  458. }
  459. return e;
  460. }
  461. typedef struct map_output_info {
  462. /* address width */
  463. int bytes;
  464. /* intnum output static data areas */
  465. unsigned char *buf;
  466. yasm_intnum *intn;
  467. /* symrec output information */
  468. unsigned long count;
  469. yasm_section *section; /* NULL for EQUs */
  470. yasm_object *object; /* object */
  471. FILE *f; /* map output file */
  472. } map_output_info;
  473. static int
  474. map_prescan_bytes(yasm_section *sect, void *d)
  475. {
  476. bin_section_data *bsd = yasm_section_get_data(sect, &bin_section_data_cb);
  477. map_output_info *info = (map_output_info *)d;
  478. assert(bsd != NULL);
  479. assert(info != NULL);
  480. while (!yasm_intnum_check_size(bsd->length, info->bytes * 8, 0, 0))
  481. info->bytes *= 2;
  482. while (!yasm_intnum_check_size(bsd->istart, info->bytes * 8, 0, 0))
  483. info->bytes *= 2;
  484. while (!yasm_intnum_check_size(bsd->ivstart, info->bytes * 8, 0, 0))
  485. info->bytes *= 2;
  486. return 0;
  487. }
  488. static void
  489. map_print_intnum(const yasm_intnum *intn, map_output_info *info)
  490. {
  491. size_t i;
  492. yasm_intnum_get_sized(intn, info->buf, info->bytes, info->bytes*8, 0, 0,
  493. 0);
  494. for (i=info->bytes; i != 0; i--)
  495. fprintf(info->f, "%02X", info->buf[i-1]);
  496. }
  497. static void
  498. map_sections_summary(bin_groups *groups, map_output_info *info)
  499. {
  500. bin_group *group;
  501. TAILQ_FOREACH(group, groups, link) {
  502. bin_section_data *bsd = group->bsd;
  503. assert(bsd != NULL);
  504. assert(info != NULL);
  505. map_print_intnum(bsd->ivstart, info);
  506. fprintf(info->f, " ");
  507. yasm_intnum_set(info->intn, bsd->ivstart);
  508. yasm_intnum_calc(info->intn, YASM_EXPR_ADD, bsd->length);
  509. map_print_intnum(info->intn, info);
  510. fprintf(info->f, " ");
  511. map_print_intnum(bsd->istart, info);
  512. fprintf(info->f, " ");
  513. yasm_intnum_set(info->intn, bsd->istart);
  514. yasm_intnum_calc(info->intn, YASM_EXPR_ADD, bsd->length);
  515. map_print_intnum(info->intn, info);
  516. fprintf(info->f, " ");
  517. map_print_intnum(bsd->length, info);
  518. fprintf(info->f, " ");
  519. fprintf(info->f, "%-*s", 10, bsd->bss ? "nobits" : "progbits");
  520. fprintf(info->f, "%s\n", yasm_section_get_name(group->section));
  521. /* Recurse to loop through follow groups */
  522. map_sections_summary(&group->follow_groups, info);
  523. }
  524. }
  525. static void
  526. map_sections_detail(bin_groups *groups, map_output_info *info)
  527. {
  528. bin_group *group;
  529. TAILQ_FOREACH(group, groups, link) {
  530. bin_section_data *bsd = group->bsd;
  531. size_t i;
  532. const char *s;
  533. s = yasm_section_get_name(group->section);
  534. fprintf(info->f, "---- Section %s ", s);
  535. for (i=0; i<(65-strlen(s)); i++)
  536. fputc('-', info->f);
  537. fprintf(info->f, "\n\nclass: %s",
  538. bsd->bss ? "nobits" : "progbits");
  539. fprintf(info->f, "\nlength: ");
  540. map_print_intnum(bsd->length, info);
  541. fprintf(info->f, "\nstart: ");
  542. map_print_intnum(bsd->istart, info);
  543. fprintf(info->f, "\nalign: ");
  544. map_print_intnum(bsd->align, info);
  545. fprintf(info->f, "\nfollows: %s",
  546. bsd->follows ? bsd->follows : "not defined");
  547. fprintf(info->f, "\nvstart: ");
  548. map_print_intnum(bsd->ivstart, info);
  549. fprintf(info->f, "\nvalign: ");
  550. map_print_intnum(bsd->valign, info);
  551. fprintf(info->f, "\nvfollows: %s\n\n",
  552. bsd->vfollows ? bsd->vfollows : "not defined");
  553. /* Recurse to loop through follow groups */
  554. map_sections_detail(&group->follow_groups, info);
  555. }
  556. }
  557. static int
  558. map_symrec_count(yasm_symrec *sym, void *d)
  559. {
  560. map_output_info *info = (map_output_info *)d;
  561. /*@dependent@*/ yasm_bytecode *precbc;
  562. assert(info != NULL);
  563. /* TODO: autodetect wider size */
  564. if (!info->section && yasm_symrec_get_equ(sym)) {
  565. info->count++;
  566. } else if (yasm_symrec_get_label(sym, &precbc) &&
  567. yasm_bc_get_section(precbc) == info->section) {
  568. info->count++;
  569. }
  570. return 0;
  571. }
  572. static int
  573. map_symrec_output(yasm_symrec *sym, void *d)
  574. {
  575. map_output_info *info = (map_output_info *)d;
  576. const yasm_expr *equ;
  577. /*@dependent@*/ yasm_bytecode *precbc;
  578. /*@only@*/ char *name = yasm_symrec_get_global_name(sym, info->object);
  579. assert(info != NULL);
  580. if (!info->section && (equ = yasm_symrec_get_equ(sym))) {
  581. yasm_expr *realequ = yasm_expr_copy(equ);
  582. realequ = yasm_expr__level_tree
  583. (realequ, 1, 1, 1, 0, bin_objfmt_expr_xform, NULL);
  584. yasm_intnum_set(info->intn, yasm_expr_get_intnum(&realequ, 0));
  585. yasm_expr_destroy(realequ);
  586. map_print_intnum(info->intn, info);
  587. fprintf(info->f, " %s\n", name);
  588. } else if (yasm_symrec_get_label(sym, &precbc) &&
  589. yasm_bc_get_section(precbc) == info->section) {
  590. bin_section_data *bsd =
  591. yasm_section_get_data(info->section, &bin_section_data_cb);
  592. /* Real address */
  593. yasm_intnum_set_uint(info->intn, yasm_bc_next_offset(precbc));
  594. yasm_intnum_calc(info->intn, YASM_EXPR_ADD, bsd->istart);
  595. map_print_intnum(info->intn, info);
  596. fprintf(info->f, " ");
  597. /* Virtual address */
  598. yasm_intnum_set_uint(info->intn, yasm_bc_next_offset(precbc));
  599. yasm_intnum_calc(info->intn, YASM_EXPR_ADD, bsd->ivstart);
  600. map_print_intnum(info->intn, info);
  601. /* Name */
  602. fprintf(info->f, " %s\n", name);
  603. }
  604. yasm_xfree(name);
  605. return 0;
  606. }
  607. static void
  608. map_sections_symbols(bin_groups *groups, map_output_info *info)
  609. {
  610. bin_group *group;
  611. TAILQ_FOREACH(group, groups, link) {
  612. info->count = 0;
  613. info->section = group->section;
  614. yasm_symtab_traverse(info->object->symtab, info, map_symrec_count);
  615. if (info->count > 0) {
  616. const char *s = yasm_section_get_name(group->section);
  617. size_t i;
  618. fprintf(info->f, "---- Section %s ", s);
  619. for (i=0; i<(65-strlen(s)); i++)
  620. fputc('-', info->f);
  621. fprintf(info->f, "\n\n%-*s%-*s%s\n",
  622. info->bytes*2+2, "Real",
  623. info->bytes*2+2, "Virtual",
  624. "Name");
  625. yasm_symtab_traverse(info->object->symtab, info,
  626. map_symrec_output);
  627. fprintf(info->f, "\n\n");
  628. }
  629. /* Recurse to loop through follow groups */
  630. map_sections_symbols(&group->follow_groups, info);
  631. }
  632. }
  633. static void
  634. output_map(bin_objfmt_output_info *info)
  635. {
  636. yasm_objfmt_bin *objfmt_bin = (yasm_objfmt_bin *)info->object->objfmt;
  637. FILE *f;
  638. int i;
  639. map_output_info mapinfo;
  640. if (objfmt_bin->map_flags == NO_MAP)
  641. return;
  642. if (objfmt_bin->map_flags == MAP_NONE)
  643. objfmt_bin->map_flags = MAP_BRIEF; /* default to brief */
  644. if (!objfmt_bin->map_filename)
  645. f = stdout; /* default to stdout */
  646. else {
  647. f = fopen(objfmt_bin->map_filename, "wt");
  648. if (!f) {
  649. yasm_warn_set(YASM_WARN_GENERAL,
  650. N_("unable to open map file `%s'"),
  651. objfmt_bin->map_filename);
  652. yasm_errwarn_propagate(info->errwarns, 0);
  653. return;
  654. }
  655. }
  656. mapinfo.object = info->object;
  657. mapinfo.f = f;
  658. /* Temporary intnum */
  659. mapinfo.intn = info->tmp_intn;
  660. /* Prescan all values to figure out what width we should make the output
  661. * fields. Start with a minimum of 4.
  662. */
  663. mapinfo.bytes = 4;
  664. while (!yasm_intnum_check_size(info->origin, mapinfo.bytes * 8, 0, 0))
  665. mapinfo.bytes *= 2;
  666. yasm_object_sections_traverse(info->object, &mapinfo, map_prescan_bytes);
  667. mapinfo.buf = yasm_xmalloc(mapinfo.bytes);
  668. fprintf(f, "\n- YASM Map file ");
  669. for (i=0; i<63; i++)
  670. fputc('-', f);
  671. fprintf(f, "\n\nSource file: %s\n", info->object->src_filename);
  672. fprintf(f, "Output file: %s\n\n", info->object->obj_filename);
  673. fprintf(f, "-- Program origin ");
  674. for (i=0; i<61; i++)
  675. fputc('-', f);
  676. fprintf(f, "\n\n");
  677. map_print_intnum(info->origin, &mapinfo);
  678. fprintf(f, "\n\n");
  679. if (objfmt_bin->map_flags & MAP_BRIEF) {
  680. fprintf(f, "-- Sections (summary) ");
  681. for (i=0; i<57; i++)
  682. fputc('-', f);
  683. fprintf(f, "\n\n%-*s%-*s%-*s%-*s%-*s%-*s%s\n",
  684. mapinfo.bytes*2+2, "Vstart",
  685. mapinfo.bytes*2+2, "Vstop",
  686. mapinfo.bytes*2+2, "Start",
  687. mapinfo.bytes*2+2, "Stop",
  688. mapinfo.bytes*2+2, "Length",
  689. 10, "Class", "Name");
  690. map_sections_summary(&info->lma_groups, &mapinfo);
  691. fprintf(f, "\n");
  692. }
  693. if (objfmt_bin->map_flags & MAP_SECTIONS) {
  694. fprintf(f, "-- Sections (detailed) ");
  695. for (i=0; i<56; i++)
  696. fputc('-', f);
  697. fprintf(f, "\n\n");
  698. map_sections_detail(&info->lma_groups, &mapinfo);
  699. }
  700. if (objfmt_bin->map_flags & MAP_SYMBOLS) {
  701. fprintf(f, "-- Symbols ");
  702. for (i=0; i<68; i++)
  703. fputc('-', f);
  704. fprintf(f, "\n\n");
  705. /* We do two passes for EQU and each section; the first pass
  706. * determines the byte width to use for the value and whether any
  707. * symbols are present, the second pass actually outputs the text.
  708. */
  709. /* EQUs */
  710. mapinfo.count = 0;
  711. mapinfo.section = NULL;
  712. yasm_symtab_traverse(info->object->symtab, &mapinfo, map_symrec_count);
  713. if (mapinfo.count > 0) {
  714. fprintf(f, "---- No Section ");
  715. for (i=0; i<63; i++)
  716. fputc('-', f);
  717. fprintf(f, "\n\n%-*s%s\n", mapinfo.bytes*2+2, "Value", "Name");
  718. yasm_symtab_traverse(info->object->symtab, &mapinfo,
  719. map_symrec_output);
  720. fprintf(f, "\n\n");
  721. }
  722. /* Other sections */
  723. map_sections_symbols(&info->lma_groups, &mapinfo);
  724. }
  725. if (f != stdout)
  726. fclose(f);
  727. yasm_xfree(mapinfo.buf);
  728. }
  729. /* Check for LMA overlap using a simple N^2 algorithm. */
  730. static int
  731. check_lma_overlap(yasm_section *sect, /*@null@*/ void *d)
  732. {
  733. bin_section_data *bsd, *bsd2;
  734. yasm_section *other = (yasm_section *)d;
  735. yasm_intnum *overlap;
  736. if (!d)
  737. return yasm_object_sections_traverse(yasm_section_get_object(sect),
  738. sect, check_lma_overlap);
  739. if (sect == other)
  740. return 0;
  741. bsd = yasm_section_get_data(sect, &bin_section_data_cb);
  742. bsd2 = yasm_section_get_data(other, &bin_section_data_cb);
  743. if (yasm_intnum_is_zero(bsd->length) ||
  744. yasm_intnum_is_zero(bsd2->length))
  745. return 0;
  746. if (yasm_intnum_compare(bsd->istart, bsd2->istart) <= 0) {
  747. overlap = yasm_intnum_copy(bsd->istart);
  748. yasm_intnum_calc(overlap, YASM_EXPR_ADD, bsd->length);
  749. yasm_intnum_calc(overlap, YASM_EXPR_SUB, bsd2->istart);
  750. } else {
  751. overlap = yasm_intnum_copy(bsd2->istart);
  752. yasm_intnum_calc(overlap, YASM_EXPR_ADD, bsd2->length);
  753. yasm_intnum_calc(overlap, YASM_EXPR_SUB, bsd->istart);
  754. }
  755. if (yasm_intnum_sign(overlap) > 0) {
  756. yasm_error_set(YASM_ERROR_GENERAL,
  757. N_("sections `%s' and `%s' overlap by %lu bytes"),
  758. yasm_section_get_name(sect),
  759. yasm_section_get_name(other),
  760. yasm_intnum_get_uint(overlap));
  761. yasm_intnum_destroy(overlap);
  762. return -1;
  763. }
  764. yasm_intnum_destroy(overlap);
  765. return 0;
  766. }
  767. static int
  768. bin_objfmt_output_value(yasm_value *value, unsigned char *buf,
  769. unsigned int destsize,
  770. /*@unused@*/ unsigned long offset, yasm_bytecode *bc,
  771. int warn, /*@null@*/ void *d)
  772. {
  773. /*@null@*/ bin_objfmt_output_info *info = (bin_objfmt_output_info *)d;
  774. /*@dependent@*/ /*@null@*/ yasm_bytecode *precbc;
  775. /*@dependent@*/ yasm_section *sect;
  776. assert(info != NULL);
  777. /* Binary objects we need to resolve against object, not against section. */
  778. if (value->rel) {
  779. unsigned int rshift = (unsigned int)value->rshift;
  780. yasm_expr *syme;
  781. /*@null@*/ const yasm_intnum *ssymval;
  782. if (yasm_symrec_is_abs(value->rel)) {
  783. syme = yasm_expr_create_ident(yasm_expr_int(
  784. yasm_intnum_create_uint(0)), bc->line);
  785. } else if (yasm_symrec_get_label(value->rel, &precbc)
  786. && (sect = yasm_bc_get_section(precbc))) {
  787. syme = yasm_expr_create_ident(yasm_expr_sym(value->rel), bc->line);
  788. } else if ((ssymval = get_ssym_value(value->rel))) {
  789. syme = yasm_expr_create_ident(yasm_expr_int(
  790. yasm_intnum_copy(ssymval)), bc->line);
  791. } else
  792. goto done;
  793. /* Handle PC-relative */
  794. if (value->curpos_rel) {
  795. yasm_expr *sube;
  796. sube = yasm_expr_create(YASM_EXPR_SUB, yasm_expr_precbc(bc),
  797. yasm_expr_int(yasm_intnum_create_uint(bc->len*bc->mult_int)),
  798. bc->line);
  799. syme = yasm_expr_create(YASM_EXPR_SUB, yasm_expr_expr(syme),
  800. yasm_expr_expr(sube), bc->line);
  801. value->curpos_rel = 0;
  802. value->ip_rel = 0;
  803. }
  804. if (value->rshift > 0)
  805. syme = yasm_expr_create(YASM_EXPR_SHR, yasm_expr_expr(syme),
  806. yasm_expr_int(yasm_intnum_create_uint(rshift)), bc->line);
  807. /* Add into absolute portion */
  808. if (!value->abs)
  809. value->abs = syme;
  810. else
  811. value->abs =
  812. yasm_expr_create(YASM_EXPR_ADD, yasm_expr_expr(value->abs),
  813. yasm_expr_expr(syme), bc->line);
  814. value->rel = NULL;
  815. value->rshift = 0;
  816. }
  817. done:
  818. /* Simplify absolute portion of value, transforming symrecs */
  819. if (value->abs)
  820. value->abs = yasm_expr__level_tree
  821. (value->abs, 1, 1, 1, 0, bin_objfmt_expr_xform, NULL);
  822. /* Output */
  823. switch (yasm_value_output_basic(value, buf, destsize, bc, warn,
  824. info->object->arch)) {
  825. case -1:
  826. return 1;
  827. case 0:
  828. break;
  829. default:
  830. return 0;
  831. }
  832. /* Couldn't output, assume it contains an external reference. */
  833. yasm_error_set(YASM_ERROR_GENERAL,
  834. N_("binary object format does not support external references"));
  835. return 1;
  836. }
  837. static int
  838. bin_objfmt_output_bytecode(yasm_bytecode *bc, /*@null@*/ void *d)
  839. {
  840. /*@null@*/ bin_objfmt_output_info *info = (bin_objfmt_output_info *)d;
  841. /*@null@*/ /*@only@*/ unsigned char *bigbuf;
  842. unsigned long size = REGULAR_OUTBUF_SIZE;
  843. int gap;
  844. assert(info != NULL);
  845. bigbuf = yasm_bc_tobytes(bc, info->buf, &size, &gap, info,
  846. bin_objfmt_output_value, NULL);
  847. /* Don't bother doing anything else if size ended up being 0. */
  848. if (size == 0) {
  849. if (bigbuf)
  850. yasm_xfree(bigbuf);
  851. return 0;
  852. }
  853. /* Warn that gaps are converted to 0 and write out the 0's. */
  854. if (gap) {
  855. unsigned long left;
  856. yasm_warn_set(YASM_WARN_UNINIT_CONTENTS,
  857. N_("uninitialized space declared in code/data section: zeroing"));
  858. /* Write out in chunks */
  859. memset(info->buf, 0, REGULAR_OUTBUF_SIZE);
  860. left = size;
  861. while (left > REGULAR_OUTBUF_SIZE) {
  862. fwrite(info->buf, REGULAR_OUTBUF_SIZE, 1, info->f);
  863. left -= REGULAR_OUTBUF_SIZE;
  864. }
  865. fwrite(info->buf, left, 1, info->f);
  866. } else {
  867. /* Output buf (or bigbuf if non-NULL) to file */
  868. fwrite(bigbuf ? bigbuf : info->buf, (size_t)size, 1, info->f);
  869. }
  870. /* If bigbuf was allocated, free it */
  871. if (bigbuf)
  872. yasm_xfree(bigbuf);
  873. return 0;
  874. }
  875. /* Check to ensure bytecode is res* (for BSS sections) */
  876. static int
  877. bin_objfmt_no_output_bytecode(yasm_bytecode *bc, /*@null@*/ void *d)
  878. {
  879. /*@null@*/ bin_objfmt_output_info *info = (bin_objfmt_output_info *)d;
  880. /*@null@*/ /*@only@*/ unsigned char *bigbuf;
  881. unsigned long size = REGULAR_OUTBUF_SIZE;
  882. int gap;
  883. assert(info != NULL);
  884. bigbuf = yasm_bc_tobytes(bc, info->buf, &size, &gap, info,
  885. bin_objfmt_output_value, NULL);
  886. /* If bigbuf was allocated, free it */
  887. if (bigbuf)
  888. yasm_xfree(bigbuf);
  889. /* Don't bother doing anything else if size ended up being 0. */
  890. if (size == 0)
  891. return 0;
  892. /* Warn if not a gap. */
  893. if (!gap) {
  894. yasm_warn_set(YASM_WARN_GENERAL,
  895. N_("initialized space declared in nobits section: ignoring"));
  896. }
  897. return 0;
  898. }
  899. static int
  900. bin_objfmt_output_section(yasm_section *sect, /*@null@*/ void *d)
  901. {
  902. bin_section_data *bsd = yasm_section_get_data(sect, &bin_section_data_cb);
  903. /*@null@*/ bin_objfmt_output_info *info = (bin_objfmt_output_info *)d;
  904. assert(bsd != NULL);
  905. assert(info != NULL);
  906. if (bsd->bss) {
  907. yasm_section_bcs_traverse(sect, info->errwarns,
  908. info, bin_objfmt_no_output_bytecode);
  909. } else {
  910. yasm_intnum_set(info->tmp_intn, bsd->istart);
  911. yasm_intnum_calc(info->tmp_intn, YASM_EXPR_SUB, info->origin);
  912. if (yasm_intnum_sign(info->tmp_intn) < 0) {
  913. yasm_error_set(YASM_ERROR_VALUE,
  914. N_("section `%s' starts before origin (ORG)"),
  915. yasm_section_get_name(sect));
  916. yasm_errwarn_propagate(info->errwarns, 0);
  917. return 0;
  918. }
  919. if (!yasm_intnum_check_size(info->tmp_intn, sizeof(long)*8, 0, 1)) {
  920. yasm_error_set(YASM_ERROR_VALUE,
  921. N_("section `%s' start value too large"),
  922. yasm_section_get_name(sect));
  923. yasm_errwarn_propagate(info->errwarns, 0);
  924. return 0;
  925. }
  926. if (fseek(info->f, yasm_intnum_get_int(info->tmp_intn) + info->start,
  927. SEEK_SET) < 0)
  928. yasm__fatal(N_("could not seek on output file"));
  929. yasm_section_bcs_traverse(sect, info->errwarns,
  930. info, bin_objfmt_output_bytecode);
  931. }
  932. return 0;
  933. }
  934. static void
  935. bin_objfmt_cleanup(bin_objfmt_output_info *info)
  936. {
  937. bin_group *group, *group_temp;
  938. yasm_xfree(info->buf);
  939. yasm_intnum_destroy(info->origin);
  940. yasm_intnum_destroy(info->tmp_intn);
  941. TAILQ_FOREACH_SAFE(group, &info->lma_groups, link, group_temp)
  942. bin_group_destroy(group);
  943. TAILQ_FOREACH_SAFE(group, &info->vma_groups, link, group_temp)
  944. bin_group_destroy(group);
  945. }
  946. static void
  947. bin_objfmt_output(yasm_object *object, FILE *f, /*@unused@*/ int all_syms,
  948. yasm_errwarns *errwarns)
  949. {
  950. yasm_objfmt_bin *objfmt_bin = (yasm_objfmt_bin *)object->objfmt;
  951. bin_objfmt_output_info info;
  952. bin_group *group, *lma_group, *vma_group, *group_temp;
  953. yasm_intnum *start, *last, *vdelta;
  954. bin_groups unsorted_groups, bss_groups;
  955. info.start = ftell(f);
  956. /* Set ORG to 0 unless otherwise specified */
  957. if (objfmt_bin->org) {
  958. info.origin = yasm_expr_get_intnum(&objfmt_bin->org, 0);
  959. if (!info.origin) {
  960. yasm_error_set(YASM_ERROR_TOO_COMPLEX,
  961. N_("ORG expression is too complex"));
  962. yasm_errwarn_propagate(errwarns, objfmt_bin->org->line);
  963. return;
  964. }
  965. if (yasm_intnum_sign(info.origin) < 0) {
  966. yasm_error_set(YASM_ERROR_VALUE, N_("ORG expression is negative"));
  967. yasm_errwarn_propagate(errwarns, objfmt_bin->org->line);
  968. return;
  969. }
  970. info.origin = yasm_intnum_copy(info.origin);
  971. } else
  972. info.origin = yasm_intnum_create_uint(0);
  973. info.object = object;
  974. info.errwarns = errwarns;
  975. info.f = f;
  976. info.buf = yasm_xmalloc(REGULAR_OUTBUF_SIZE);
  977. info.tmp_intn = yasm_intnum_create_uint(0);
  978. TAILQ_INIT(&info.lma_groups);
  979. TAILQ_INIT(&info.vma_groups);
  980. /* Check symbol table */
  981. yasm_symtab_traverse(object->symtab, &info, bin_objfmt_check_sym);
  982. /* Create section groups */
  983. if (yasm_object_sections_traverse(object, &info, bin_lma_create_group)) {
  984. bin_objfmt_cleanup(&info);
  985. return; /* error detected */
  986. }
  987. /* Determine section order according to LMA.
  988. * Sections can be ordered either by (priority):
  989. * - follows
  990. * - start
  991. * - progbits/nobits setting
  992. * - order in the input file
  993. */
  994. /* Look at each group with follows specified, and find the section
  995. * that group is supposed to follow.
  996. */
  997. TAILQ_FOREACH_SAFE(lma_group, &info.lma_groups, link, group_temp) {
  998. if (lma_group->bsd->follows) {
  999. bin_group *found;
  1000. /* Need to find group containing section this section follows. */
  1001. found =
  1002. find_group_by_name(&info.lma_groups, lma_group->bsd->follows);
  1003. if (!found) {
  1004. yasm_error_set(YASM_ERROR_VALUE,
  1005. N_("section `%s' follows an invalid or unknown section `%s'"),
  1006. yasm_section_get_name(lma_group->section),
  1007. lma_group->bsd->follows);
  1008. yasm_errwarn_propagate(errwarns, 0);
  1009. bin_objfmt_cleanup(&info);
  1010. return;
  1011. }
  1012. /* Check for loops */
  1013. if (lma_group->section == found->section ||
  1014. find_group_by_section(&lma_group->follow_groups,
  1015. found->section)) {
  1016. yasm_error_set(YASM_ERROR_VALUE,
  1017. N_("follows loop between section `%s' and section `%s'"),
  1018. yasm_section_get_name(lma_group->section),
  1019. yasm_section_get_name(found->section));
  1020. yasm_errwarn_propagate(errwarns, 0);
  1021. bin_objfmt_cleanup(&info);
  1022. return;
  1023. }
  1024. /* Remove this section from main lma groups list */
  1025. TAILQ_REMOVE(&info.lma_groups, lma_group, link);
  1026. /* Add it after the section it's supposed to follow. */
  1027. TAILQ_INSERT_TAIL(&found->follow_groups, lma_group, link);
  1028. }
  1029. }
  1030. /* Sort the top-level groups according to their start address.
  1031. * Use Shell sort for ease of implementation.
  1032. * If no start address is specified for a section, don't change the order,
  1033. * and move BSS sections to a separate list so they can be moved to the
  1034. * end of the lma list after all other sections are sorted.
  1035. */
  1036. unsorted_groups = info.lma_groups; /* structure copy */
  1037. TAILQ_INIT(&info.lma_groups);
  1038. TAILQ_INIT(&bss_groups);
  1039. TAILQ_FOREACH_SAFE(lma_group, &unsorted_groups, link, group_temp) {
  1040. bin_group *before;
  1041. if (!lma_group->bsd->istart) {
  1042. if (lma_group->bsd->bss)
  1043. TAILQ_INSERT_TAIL(&bss_groups, lma_group, link);
  1044. else
  1045. TAILQ_INSERT_TAIL(&info.lma_groups, lma_group, link);
  1046. continue;
  1047. }
  1048. before = NULL;
  1049. TAILQ_FOREACH(group, &info.lma_groups, link) {
  1050. if (!group->bsd->istart)
  1051. continue;
  1052. if (yasm_intnum_compare(group->bsd->istart,
  1053. lma_group->bsd->istart) > 0) {
  1054. before = group;
  1055. break;
  1056. }
  1057. }
  1058. if (before)
  1059. TAILQ_INSERT_BEFORE(before, lma_group, link);
  1060. else
  1061. TAILQ_INSERT_TAIL(&info.lma_groups, lma_group, link);
  1062. }
  1063. /* Move the pure-BSS sections to the end of the LMA list. */
  1064. TAILQ_FOREACH_SAFE(group, &bss_groups, link, group_temp)
  1065. TAILQ_INSERT_TAIL(&info.lma_groups, group, link);
  1066. TAILQ_INIT(&bss_groups); /* For sanity */
  1067. /* Assign a LMA start address to every section.
  1068. * Also assign VMA=LMA unless otherwise specified.
  1069. *
  1070. * We need to assign VMA=LMA here (while walking the tree) for the case:
  1071. * sect1 start=0 (size=0x11)
  1072. * sect2 follows=sect1 valign=16 (size=0x104)
  1073. * sect3 follows=sect2 valign=16
  1074. * Where the valign of sect2 will result in a sect3 vaddr higher than a
  1075. * naive segment-by-segment interpretation (where sect3 and sect2 would
  1076. * have a VMA overlap).
  1077. *
  1078. * Algorithm for VMA=LMA setting:
  1079. * Start with delta=0.
  1080. * If there's no virtual attributes, we simply set VMA = LMA+delta.
  1081. * If there's only valign specified, we set VMA = aligned LMA, and add
  1082. * any new alignment difference to delta.
  1083. *
  1084. * We could do the LMA start and VMA=LMA steps in two separate steps,
  1085. * but it's easier to just recurse once.
  1086. */
  1087. start = yasm_intnum_copy(info.origin);
  1088. last = yasm_intnum_copy(info.origin);
  1089. vdelta = yasm_intnum_create_uint(0);
  1090. TAILQ_FOREACH(lma_group, &info.lma_groups, link) {
  1091. if (lma_group->bsd->istart)
  1092. yasm_intnum_set(start, lma_group->bsd->istart);
  1093. group_assign_start_recurse(lma_group, start, last, vdelta,
  1094. info.tmp_intn, errwarns);
  1095. yasm_intnum_set(start, last);
  1096. }
  1097. yasm_intnum_destroy(last);
  1098. yasm_intnum_destroy(vdelta);
  1099. /*
  1100. * Determine section order according to VMA
  1101. */
  1102. /* Create section groups */
  1103. if (yasm_object_sections_traverse(object, &info, bin_vma_create_group)) {
  1104. yasm_intnum_destroy(start);
  1105. bin_objfmt_cleanup(&info);
  1106. return; /* error detected */
  1107. }
  1108. /* Look at each group with vfollows specified, and find the section
  1109. * that group is supposed to follow.
  1110. */
  1111. TAILQ_FOREACH_SAFE(vma_group, &info.vma_groups, link, group_temp) {
  1112. if (vma_group->bsd->vfollows) {
  1113. bin_group *found;
  1114. /* Need to find group containing section this section follows. */
  1115. found = find_group_by_name(&info.vma_groups,
  1116. vma_group->bsd->vfollows);
  1117. if (!found) {
  1118. yasm_error_set(YASM_ERROR_VALUE,
  1119. N_("section `%s' vfollows an invalid or unknown section `%s'"),
  1120. yasm_section_get_name(vma_group->section),
  1121. vma_group->bsd->vfollows);
  1122. yasm_errwarn_propagate(errwarns, 0);
  1123. yasm_intnum_destroy(start);
  1124. bin_objfmt_cleanup(&info);
  1125. return;
  1126. }
  1127. /* Check for loops */
  1128. if (vma_group->section == found->section ||
  1129. find_group_by_section(&vma_group->follow_groups,
  1130. found->section)) {
  1131. yasm_error_set(YASM_ERROR_VALUE,
  1132. N_("vfollows loop between section `%s' and section `%s'"),
  1133. yasm_section_get_name(vma_group->section),
  1134. yasm_section_get_name(found->section));
  1135. yasm_errwarn_propagate(errwarns, 0);
  1136. bin_objfmt_cleanup(&info);
  1137. return;
  1138. }
  1139. /* Remove this section from main lma groups list */
  1140. TAILQ_REMOVE(&info.vma_groups, vma_group, link);
  1141. /* Add it after the section it's supposed to follow. */
  1142. TAILQ_INSERT_TAIL(&found->follow_groups, vma_group, link);
  1143. }
  1144. }
  1145. /* Due to the combination of steps above, we now know that all top-level
  1146. * groups have integer ivstart:
  1147. * Vstart Vfollows Valign Handled by
  1148. * No No No group_assign_start_recurse()
  1149. * No No Yes group_assign_start_recurse()
  1150. * No Yes - vfollows loop (above)
  1151. * Yes - - bin_lma_create_group()
  1152. */
  1153. TAILQ_FOREACH(vma_group, &info.vma_groups, link) {
  1154. yasm_intnum_set(start, vma_group->bsd->ivstart);
  1155. group_assign_vstart_recurse(vma_group, start, errwarns);
  1156. }
  1157. /* Output map file */
  1158. output_map(&info);
  1159. /* Ensure we don't have overlapping progbits LMAs.
  1160. * Use a dumb O(N^2) algorithm as the number of sections is essentially
  1161. * always low.
  1162. */
  1163. if (yasm_object_sections_traverse(object, NULL, check_lma_overlap)) {
  1164. yasm_errwarn_propagate(errwarns, 0);
  1165. yasm_intnum_destroy(start);
  1166. bin_objfmt_cleanup(&info);
  1167. return;
  1168. }
  1169. /* Output sections */
  1170. yasm_object_sections_traverse(object, &info, bin_objfmt_output_section);
  1171. /* Clean up */
  1172. yasm_intnum_destroy(start);
  1173. bin_objfmt_cleanup(&info);
  1174. }
  1175. static void
  1176. bin_objfmt_destroy(yasm_objfmt *objfmt)
  1177. {
  1178. yasm_objfmt_bin *objfmt_bin = (yasm_objfmt_bin *)objfmt;
  1179. if (objfmt_bin->map_filename)
  1180. yasm_xfree(objfmt_bin->map_filename);
  1181. yasm_expr_destroy(objfmt_bin->org);
  1182. yasm_xfree(objfmt);
  1183. }
  1184. static void
  1185. define_section_symbol(yasm_symtab *symtab, yasm_section *sect,
  1186. const char *sectname, const char *suffix,
  1187. enum bin_ssym which, unsigned long line)
  1188. {
  1189. yasm_symrec *sym;
  1190. bin_symrec_data *bsymd = yasm_xmalloc(sizeof(bin_symrec_data));
  1191. char *symname = yasm_xmalloc(8+strlen(sectname)+strlen(suffix)+1);
  1192. strcpy(symname, "section.");
  1193. strcat(symname, sectname);
  1194. strcat(symname, suffix);
  1195. bsymd->section = sect;
  1196. bsymd->which = which;
  1197. sym = yasm_symtab_declare(symtab, symname, YASM_SYM_EXTERN, line);
  1198. yasm_xfree(symname);
  1199. yasm_symrec_add_data(sym, &bin_symrec_data_cb, bsymd);
  1200. }
  1201. static void
  1202. bin_objfmt_init_new_section(yasm_section *sect, unsigned long line)
  1203. {
  1204. yasm_object *object = yasm_section_get_object(sect);
  1205. const char *sectname = yasm_section_get_name(sect);
  1206. /*yasm_objfmt_bin *objfmt_bin = (yasm_objfmt_bin *)object->objfmt;*/
  1207. bin_section_data *data;
  1208. data = yasm_xmalloc(sizeof(bin_section_data));
  1209. data->bss = 0;
  1210. data->align = NULL;
  1211. data->valign = NULL;
  1212. data->start = NULL;
  1213. data->vstart = NULL;
  1214. data->follows = NULL;
  1215. data->vfollows = NULL;
  1216. data->istart = NULL;
  1217. data->ivstart = NULL;
  1218. data->length = NULL;
  1219. yasm_section_add_data(sect, &bin_section_data_cb, data);
  1220. define_section_symbol(object->symtab, sect, sectname, ".start",
  1221. SSYM_START, line);
  1222. define_section_symbol(object->symtab, sect, sectname, ".vstart",
  1223. SSYM_VSTART, line);
  1224. define_section_symbol(object->symtab, sect, sectname, ".length",
  1225. SSYM_LENGTH, line);
  1226. }
  1227. static yasm_section *
  1228. bin_objfmt_add_default_section(yasm_object *object)
  1229. {
  1230. yasm_section *retval;
  1231. int isnew;
  1232. retval = yasm_object_get_general(object, ".text", 0, 1, 0, &isnew, 0);
  1233. if (isnew)
  1234. yasm_section_set_default(retval, 1);
  1235. return retval;
  1236. }
  1237. /* GAS-style flags */
  1238. static int
  1239. bin_helper_gasflags(void *obj, yasm_valparam *vp, unsigned long line, void *d,
  1240. /*@unused@*/ uintptr_t arg)
  1241. {
  1242. /* TODO */
  1243. return 0;
  1244. }
  1245. static /*@observer@*/ /*@null@*/ yasm_section *
  1246. bin_objfmt_section_switch(yasm_object *object, yasm_valparamhead *valparams,
  1247. /*@unused@*/ /*@null@*/
  1248. yasm_valparamhead *objext_valparams,
  1249. unsigned long line)
  1250. {
  1251. yasm_valparam *vp;
  1252. yasm_section *retval;
  1253. int isnew;
  1254. int flags_override = 0;
  1255. const char *sectname;
  1256. bin_section_data *bsd = NULL;
  1257. struct bin_section_switch_data {
  1258. /*@only@*/ /*@null@*/ char *follows;
  1259. /*@only@*/ /*@null@*/ char *vfollows;
  1260. /*@only@*/ /*@null@*/ yasm_expr *start;
  1261. /*@only@*/ /*@null@*/ yasm_expr *vstart;
  1262. /*@only@*/ /*@null@*/ yasm_intnum *align;
  1263. /*@only@*/ /*@null@*/ yasm_intnum *valign;
  1264. unsigned long bss;
  1265. unsigned long code;
  1266. } data;
  1267. static const yasm_dir_help help[] = {
  1268. { "follows", 1, yasm_dir_helper_string,
  1269. offsetof(struct bin_section_switch_data, follows), 0 },
  1270. { "vfollows", 1, yasm_dir_helper_string,
  1271. offsetof(struct bin_section_switch_data, vfollows), 0 },
  1272. { "start", 1, yasm_dir_helper_expr,
  1273. offsetof(struct bin_section_switch_data, start), 0 },
  1274. { "vstart", 1, yasm_dir_helper_expr,
  1275. offsetof(struct bin_section_switch_data, vstart), 0 },
  1276. { "align", 1, yasm_dir_helper_intn,
  1277. offsetof(struct bin_section_switch_data, align), 0 },
  1278. { "valign", 1, yasm_dir_helper_intn,
  1279. offsetof(struct bin_section_switch_data, valign), 0 },
  1280. { "nobits", 0, yasm_dir_helper_flag_set,
  1281. offsetof(struct bin_section_switch_data, bss), 1 },
  1282. { "progbits", 0, yasm_dir_helper_flag_set,
  1283. offsetof(struct bin_section_switch_data, bss), 0 },
  1284. { "code", 0, yasm_dir_helper_flag_set,
  1285. offsetof(struct bin_section_switch_data, code), 1 },
  1286. { "data", 0, yasm_dir_helper_flag_set,
  1287. offsetof(struct bin_section_switch_data, code), 0 },
  1288. { "execute", 0, yasm_dir_helper_flag_set,
  1289. offsetof(struct bin_section_switch_data, code), 1 },
  1290. { "noexecute", 0, yasm_dir_helper_flag_set,
  1291. offsetof(struct bin_section_switch_data, code), 0 },
  1292. { "gasflags", 1, bin_helper_gasflags, 0, 0 }
  1293. };
  1294. vp = yasm_vps_first(valparams);
  1295. sectname = yasm_vp_string(vp);
  1296. if (!sectname)
  1297. return NULL;
  1298. vp = yasm_vps_next(vp);
  1299. retval = yasm_object_find_general(object, sectname);
  1300. if (retval) {
  1301. bsd = yasm_section_get_data(retval, &bin_section_data_cb);
  1302. assert(bsd != NULL);
  1303. data.follows = bsd->follows;
  1304. data.vfollows = bsd->vfollows;
  1305. data.start = bsd->start;
  1306. data.vstart = bsd->vstart;
  1307. data.align = NULL;
  1308. data.valign = NULL;
  1309. data.bss = bsd->bss;
  1310. data.code = yasm_section_is_code(retval);
  1311. } else {
  1312. data.follows = NULL;
  1313. data.vfollows = NULL;
  1314. data.start = NULL;
  1315. data.vstart = NULL;
  1316. data.align = NULL;
  1317. data.valign = NULL;
  1318. data.bss = strcmp(sectname, ".bss") == 0;
  1319. data.code = strcmp(sectname, ".text") == 0;
  1320. }
  1321. flags_override = yasm_dir_helper(object, vp, line, help, NELEMS(help),
  1322. &data, yasm_dir_helper_valparam_warn);
  1323. if (flags_override < 0)
  1324. return NULL; /* error occurred */
  1325. if (data.start && data.follows) {
  1326. yasm_error_set(YASM_ERROR_GENERAL,
  1327. N_("cannot combine `start' and `follows' section attributes"));
  1328. return NULL;
  1329. }
  1330. if (data.vstart && data.vfollows) {
  1331. yasm_error_set(YASM_ERROR_GENERAL,
  1332. N_("cannot combine `vstart' and `vfollows' section attributes"));
  1333. return NULL;
  1334. }
  1335. if (data.align) {
  1336. unsigned long align = yasm_intnum_get_uint(data.align);
  1337. /* Alignments must be a power of two. */
  1338. if (!is_exp2(align)) {
  1339. yasm_error_set(YASM_ERROR_VALUE,
  1340. N_("argument to `%s' is not a power of two"),
  1341. "align");
  1342. return NULL;
  1343. }
  1344. } else
  1345. data.align = bsd ? bsd->align : NULL;
  1346. if (data.valign) {
  1347. unsigned long valign = yasm_intnum_get_uint(data.valign);
  1348. /* Alignments must be a power of two. */
  1349. if (!is_exp2(valign)) {
  1350. yasm_error_set(YASM_ERROR_VALUE,
  1351. N_("argument to `%s' is not a power of two"),
  1352. "valign");
  1353. return NULL;
  1354. }
  1355. } else
  1356. data.valign = bsd ? bsd->valign : NULL;
  1357. retval = yasm_object_get_general(object, sectname, 0, (int)data.code,
  1358. (int)data.bss, &isnew, line);
  1359. bsd = yasm_section_get_data(retval, &bin_section_data_cb);
  1360. if (isnew || yasm_section_is_default(retval)) {
  1361. yasm_section_set_default(retval, 0);
  1362. }
  1363. /* Update section flags */
  1364. bsd->bss = data.bss;
  1365. bsd->align = data.align;
  1366. bsd->valign = data.valign;
  1367. bsd->start = data.start;
  1368. bsd->vstart = data.vstart;
  1369. bsd->follows = data.follows;
  1370. bsd->vfollows = data.vfollows;
  1371. return retval;
  1372. }
  1373. static /*@observer@*/ /*@null@*/ yasm_symrec *
  1374. bin_objfmt_get_special_sym(yasm_object *object, const char *name,
  1375. const char *parser)
  1376. {
  1377. return NULL;
  1378. }
  1379. static void
  1380. bin_objfmt_dir_org(yasm_object *object,
  1381. /*@null@*/ yasm_valparamhead *valparams,
  1382. /*@unused@*/ /*@null@*/
  1383. yasm_valparamhead *objext_valparams, unsigned long line)
  1384. {
  1385. yasm_objfmt_bin *objfmt_bin = (yasm_objfmt_bin *)object->objfmt;
  1386. yasm_valparam *vp;
  1387. /* We only allow a single ORG in a program. */
  1388. if (objfmt_bin->org) {
  1389. yasm_error_set(YASM_ERROR_GENERAL, N_("program origin redefined"));
  1390. return;
  1391. }
  1392. /* ORG takes just a simple expression as param */
  1393. vp = yasm_vps_first(valparams);
  1394. objfmt_bin->org = yasm_vp_expr(vp, object->symtab, line);
  1395. if (!objfmt_bin->org) {
  1396. yasm_error_set(YASM_ERROR_SYNTAX,
  1397. N_("argument to ORG must be expression"));
  1398. return;
  1399. }
  1400. }
  1401. struct bin_dir_map_data {
  1402. unsigned long flags;
  1403. /*@only@*/ /*@null@*/ char *filename;
  1404. };
  1405. static int
  1406. dir_map_filename(void *obj, yasm_valparam *vp, unsigned long line, void *data)
  1407. {
  1408. struct bin_dir_map_data *mdata = (struct bin_dir_map_data *)data;
  1409. const char *filename;
  1410. if (mdata->filename) {
  1411. yasm_warn_set(YASM_WARN_GENERAL, N_("map file already specified"));
  1412. return 0;
  1413. }
  1414. filename = yasm_vp_string(vp);
  1415. if (!filename) {
  1416. yasm_error_set(YASM_ERROR_SYNTAX,
  1417. N_("unexpected expression in [map]"));
  1418. return -1;
  1419. }
  1420. mdata->filename = yasm__xstrdup(filename);
  1421. return 1;
  1422. }
  1423. static void
  1424. bin_objfmt_dir_map(yasm_object *object,
  1425. /*@null@*/ yasm_valparamhead *valparams,
  1426. /*@unused@*/ /*@null@*/
  1427. yasm_valparamhead *objext_valparams, unsigned long line)
  1428. {
  1429. yasm_objfmt_bin *objfmt_bin = (yasm_objfmt_bin *)object->objfmt;
  1430. struct bin_dir_map_data data;
  1431. static const yasm_dir_help help[] = {
  1432. { "all", 0, yasm_dir_helper_flag_or,
  1433. offsetof(struct bin_dir_map_data, flags),
  1434. MAP_BRIEF|MAP_SECTIONS|MAP_SYMBOLS },
  1435. { "brief", 0, yasm_dir_helper_flag_or,
  1436. offsetof(struct bin_dir_map_data, flags), MAP_BRIEF },
  1437. { "sections", 0, yasm_dir_helper_flag_or,
  1438. offsetof(struct bin_dir_map_data, flags), MAP_SECTIONS },
  1439. { "segments", 0, yasm_dir_helper_flag_or,
  1440. offsetof(struct bin_dir_map_data, flags), MAP_SECTIONS },
  1441. { "symbols", 0, yasm_dir_helper_flag_or,
  1442. offsetof(struct bin_dir_map_data, flags), MAP_SYMBOLS }
  1443. };
  1444. data.flags = objfmt_bin->map_flags | MAP_NONE;
  1445. data.filename = objfmt_bin->map_filename;
  1446. if (valparams && yasm_dir_helper(object, yasm_vps_first(valparams), line, help,
  1447. NELEMS(help), &data, dir_map_filename) < 0)
  1448. return; /* error occurred */
  1449. objfmt_bin->map_flags = data.flags;
  1450. objfmt_bin->map_filename = data.filename;
  1451. }
  1452. static void
  1453. bin_section_data_destroy(void *data)
  1454. {
  1455. bin_section_data *bsd = (bin_section_data *)data;
  1456. if (bsd->start)
  1457. yasm_expr_destroy(bsd->start);
  1458. if (bsd->vstart)
  1459. yasm_expr_destroy(bsd->vstart);
  1460. if (bsd->follows)
  1461. yasm_xfree(bsd->follows);
  1462. if (bsd->vfollows)
  1463. yasm_xfree(bsd->vfollows);
  1464. if (bsd->istart)
  1465. yasm_intnum_destroy(bsd->istart);
  1466. if (bsd->ivstart)
  1467. yasm_intnum_destroy(bsd->ivstart);
  1468. if (bsd->length)
  1469. yasm_intnum_destroy(bsd->length);
  1470. yasm_xfree(data);
  1471. }
  1472. static void
  1473. bin_section_data_print(void *data, FILE *f, int indent_level)
  1474. {
  1475. bin_section_data *bsd = (bin_section_data *)data;
  1476. fprintf(f, "%*sbss=%d\n", indent_level, "", bsd->bss);
  1477. fprintf(f, "%*salign=", indent_level, "");
  1478. if (bsd->align)
  1479. yasm_intnum_print(bsd->align, f);
  1480. else
  1481. fprintf(f, "(nil)");
  1482. fprintf(f, "\n%*svalign=", indent_level, "");
  1483. if (bsd->valign)
  1484. yasm_intnum_print(bsd->valign, f);
  1485. else
  1486. fprintf(f, "(nil)");
  1487. fprintf(f, "\n%*sstart=", indent_level, "");
  1488. yasm_expr_print(bsd->start, f);
  1489. fprintf(f, "\n%*svstart=", indent_level, "");
  1490. yasm_expr_print(bsd->vstart, f);
  1491. fprintf(f, "\n%*sfollows=", indent_level, "");
  1492. if (bsd->follows)
  1493. fprintf(f, "\"%s\"", bsd->follows);
  1494. else
  1495. fprintf(f, "(nil)");
  1496. fprintf(f, "\n%*svfollows=", indent_level, "");
  1497. if (bsd->vfollows)
  1498. fprintf(f, "\"%s\"", bsd->vfollows);
  1499. else
  1500. fprintf(f, "(nil)");
  1501. fprintf(f, "\n%*sistart=", indent_level, "");
  1502. if (bsd->istart)
  1503. yasm_intnum_print(bsd->istart, f);
  1504. else
  1505. fprintf(f, "(nil)");
  1506. fprintf(f, "\n%*sivstart=", indent_level, "");
  1507. if (bsd->ivstart)
  1508. yasm_intnum_print(bsd->ivstart, f);
  1509. else
  1510. fprintf(f, "(nil)");
  1511. fprintf(f, "\n%*slength=", indent_level, "");
  1512. if (bsd->length)
  1513. yasm_intnum_print(bsd->length, f);
  1514. else
  1515. fprintf(f, "(nil)");
  1516. fprintf(f, "\n");
  1517. }
  1518. static void
  1519. bin_symrec_data_destroy(void *data)
  1520. {
  1521. yasm_xfree(data);
  1522. }
  1523. static void
  1524. bin_symrec_data_print(void *data, FILE *f, int indent_level)
  1525. {
  1526. bin_symrec_data *bsymd = (bin_symrec_data *)data;
  1527. fprintf(f, "%*ssection=\"%s\"\n", indent_level, "",
  1528. yasm_section_get_name(bsymd->section));
  1529. fprintf(f, "%*swhich=", indent_level, "");
  1530. switch (bsymd->which) {
  1531. case SSYM_START: fprintf(f, "START"); break;
  1532. case SSYM_VSTART: fprintf(f, "VSTART"); break;
  1533. case SSYM_LENGTH: fprintf(f, "LENGTH"); break;
  1534. }
  1535. fprintf(f, "\n");
  1536. }
  1537. /* Define valid debug formats to use with this object format */
  1538. static const char *bin_objfmt_dbgfmt_keywords[] = {
  1539. "null",
  1540. NULL
  1541. };
  1542. static const yasm_directive bin_objfmt_directives[] = {
  1543. { "org", "nasm", bin_objfmt_dir_org, YASM_DIR_ARG_REQUIRED },
  1544. { "map", "nasm", bin_objfmt_dir_map, YASM_DIR_ANY },
  1545. { NULL, NULL, NULL, 0 }
  1546. };
  1547. static const char *bin_nasm_stdmac[] = {
  1548. "%imacro org 1+.nolist",
  1549. "[org %1]",
  1550. "%endmacro",
  1551. NULL
  1552. };
  1553. static const yasm_stdmac bin_objfmt_stdmacs[] = {
  1554. { "nasm", "nasm", bin_nasm_stdmac },
  1555. { "tasm", "tasm", bin_nasm_stdmac },
  1556. { NULL, NULL, NULL }
  1557. };
  1558. /* Define objfmt structure -- see objfmt.h for details */
  1559. yasm_objfmt_module yasm_bin_LTX_objfmt = {
  1560. "Flat format binary",
  1561. "bin",
  1562. NULL,
  1563. 16,
  1564. 0,
  1565. bin_objfmt_dbgfmt_keywords,
  1566. "null",
  1567. bin_objfmt_directives,
  1568. bin_objfmt_stdmacs,
  1569. bin_objfmt_create,
  1570. bin_objfmt_output,
  1571. bin_objfmt_destroy,
  1572. bin_objfmt_add_default_section,
  1573. bin_objfmt_init_new_section,
  1574. bin_objfmt_section_switch,
  1575. bin_objfmt_get_special_sym
  1576. };
  1577. #define EXE_HEADER_SIZE 0x200
  1578. /* DOS .EXE binaries are just raw binaries with a header */
  1579. yasm_objfmt_module yasm_dosexe_LTX_objfmt;
  1580. static yasm_objfmt *
  1581. dosexe_objfmt_create(yasm_object *object)
  1582. {
  1583. yasm_objfmt_bin *objfmt_bin = (yasm_objfmt_bin *) bin_objfmt_create(object);
  1584. objfmt_bin->objfmt.module = &yasm_dosexe_LTX_objfmt;
  1585. return (yasm_objfmt *)objfmt_bin;
  1586. }
  1587. static unsigned long
  1588. get_sym(yasm_object *object, const char *name) {
  1589. yasm_symrec *symrec = yasm_symtab_get(object->symtab, name);
  1590. yasm_bytecode *prevbc;
  1591. if (!symrec)
  1592. return 0;
  1593. if (!yasm_symrec_get_label(symrec, &prevbc))
  1594. return 0;
  1595. return prevbc->offset + prevbc->len;
  1596. }
  1597. static void
  1598. dosexe_objfmt_output(yasm_object *object, FILE *f, /*@unused@*/ int all_syms,
  1599. yasm_errwarns *errwarns)
  1600. {
  1601. unsigned long tot_size, size, bss_size;
  1602. unsigned long start, bss;
  1603. unsigned char c;
  1604. fseek(f, EXE_HEADER_SIZE, SEEK_SET);
  1605. bin_objfmt_output(object, f, all_syms, errwarns);
  1606. tot_size = ftell(f);
  1607. /* if there is a __bss_start symbol, data after it is 0, no need to write
  1608. * it. */
  1609. bss = get_sym(object, "__bss_start");
  1610. if (bss)
  1611. size = bss;
  1612. else
  1613. size = tot_size;
  1614. bss_size = tot_size - size;
  1615. #ifdef HAVE_FTRUNCATE
  1616. if (size != tot_size)
  1617. ftruncate(fileno(f), EXE_HEADER_SIZE + size);
  1618. #endif
  1619. fseek(f, 0, SEEK_SET);
  1620. /* magic */
  1621. fwrite("MZ", 1, 2, f);
  1622. /* file size */
  1623. c = size & 0xff;
  1624. fwrite(&c, 1, 1, f);
  1625. c = !!(size & 0x100);
  1626. fwrite(&c, 1, 1, f);
  1627. c = ((size + 511) >> 9) & 0xff;
  1628. fwrite(&c, 1, 1, f);
  1629. c = ((size + 511) >> 17) & 0xff;
  1630. fwrite(&c, 1, 1, f);
  1631. /* relocation # */
  1632. c = 0;
  1633. fwrite(&c, 1, 1, f);
  1634. fwrite(&c, 1, 1, f);
  1635. /* header size */
  1636. c = EXE_HEADER_SIZE / 16;
  1637. fwrite(&c, 1, 1, f);
  1638. c = 0;
  1639. fwrite(&c, 1, 1, f);
  1640. /* minimum paragraph # */
  1641. bss_size = (bss_size + 15) >> 4;
  1642. c = bss_size & 0xff;
  1643. fwrite(&c, 1, 1, f);
  1644. c = (bss_size >> 8) & 0xff;
  1645. fwrite(&c, 1, 1, f);
  1646. /* maximum paragraph # */
  1647. c = 0xFF;
  1648. fwrite(&c, 1, 1, f);
  1649. fwrite(&c, 1, 1, f);
  1650. /* relative value of stack segment */
  1651. c = 0;
  1652. fwrite(&c, 1, 1, f);
  1653. fwrite(&c, 1, 1, f);
  1654. /* SP at start */
  1655. c = 0;
  1656. fwrite(&c, 1, 1, f);
  1657. fwrite(&c, 1, 1, f);
  1658. /* header checksum */
  1659. c = 0;
  1660. fwrite(&c, 1, 1, f);
  1661. fwrite(&c, 1, 1, f);
  1662. /* IP at start */
  1663. start = get_sym(object, "start");
  1664. if (!start) {
  1665. yasm_error_set(YASM_ERROR_GENERAL,
  1666. N_("%s: could not find symbol `start'"));
  1667. return;
  1668. }
  1669. c = start & 0xff;
  1670. fwrite(&c, 1, 1, f);
  1671. c = (start >> 8) & 0xff;
  1672. fwrite(&c, 1, 1, f);
  1673. /* CS start */
  1674. c = 0;
  1675. fwrite(&c, 1, 1, f);
  1676. fwrite(&c, 1, 1, f);
  1677. /* reloc start */
  1678. c = 0x22;
  1679. fwrite(&c, 1, 1, f);
  1680. c = 0;
  1681. fwrite(&c, 1, 1, f);
  1682. /* Overlay number */
  1683. c = 0;
  1684. fwrite(&c, 1, 1, f);
  1685. fwrite(&c, 1, 1, f);
  1686. }
  1687. /* Define objfmt structure -- see objfmt.h for details */
  1688. yasm_objfmt_module yasm_dosexe_LTX_objfmt = {
  1689. "DOS .EXE format binary",
  1690. "dosexe",
  1691. "exe",
  1692. 16,
  1693. 0,
  1694. bin_objfmt_dbgfmt_keywords,
  1695. "null",
  1696. bin_objfmt_directives,
  1697. bin_objfmt_stdmacs,
  1698. dosexe_objfmt_create,
  1699. dosexe_objfmt_output,
  1700. bin_objfmt_destroy,
  1701. bin_objfmt_add_default_section,
  1702. bin_objfmt_init_new_section,
  1703. bin_objfmt_section_switch,
  1704. bin_objfmt_get_special_sym
  1705. };