outmacho.c 71 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488
  1. /* ----------------------------------------------------------------------- *
  2. *
  3. * Copyright 1996-2018 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. * outmacho.c output routines for the Netwide Assembler to produce
  35. * NeXTstep/OpenStep/Rhapsody/Darwin/MacOS X 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 "nasm.h"
  43. #include "nasmlib.h"
  44. #include "ilog2.h"
  45. #include "labels.h"
  46. #include "error.h"
  47. #include "saa.h"
  48. #include "raa.h"
  49. #include "rbtree.h"
  50. #include "hashtbl.h"
  51. #include "outform.h"
  52. #include "outlib.h"
  53. #include "ver.h"
  54. #include "dwarf.h"
  55. #if defined(OF_MACHO) || defined(OF_MACHO64)
  56. /* Mach-O in-file header structure sizes */
  57. #define MACHO_HEADER_SIZE 28
  58. #define MACHO_SEGCMD_SIZE 56
  59. #define MACHO_SECTCMD_SIZE 68
  60. #define MACHO_SYMCMD_SIZE 24
  61. #define MACHO_NLIST_SIZE 12
  62. #define MACHO_RELINFO_SIZE 8
  63. #define MACHO_HEADER64_SIZE 32
  64. #define MACHO_SEGCMD64_SIZE 72
  65. #define MACHO_SECTCMD64_SIZE 80
  66. #define MACHO_NLIST64_SIZE 16
  67. /* Mach-O file header values */
  68. #define MH_MAGIC 0xfeedface
  69. #define MH_MAGIC_64 0xfeedfacf
  70. #define CPU_TYPE_I386 7 /* x86 platform */
  71. #define CPU_TYPE_X86_64 0x01000007 /* x86-64 platform */
  72. #define CPU_SUBTYPE_I386_ALL 3 /* all-x86 compatible */
  73. #define MH_OBJECT 0x1 /* object file */
  74. /* Mach-O header flags */
  75. #define MH_SUBSECTIONS_VIA_SYMBOLS 0x2000
  76. /* Mach-O load commands */
  77. #define LC_SEGMENT 0x1 /* 32-bit segment load cmd */
  78. #define LC_SEGMENT_64 0x19 /* 64-bit segment load cmd */
  79. #define LC_SYMTAB 0x2 /* symbol table load command */
  80. /* Mach-O relocations numbers */
  81. /* Generic relocs, used by i386 Mach-O */
  82. #define GENERIC_RELOC_VANILLA 0 /* Generic relocation */
  83. #define GENERIC_RELOC_TLV 5 /* Thread local */
  84. #define X86_64_RELOC_UNSIGNED 0 /* Absolute address */
  85. #define X86_64_RELOC_SIGNED 1 /* Signed 32-bit disp */
  86. #define X86_64_RELOC_BRANCH 2 /* CALL/JMP with 32-bit disp */
  87. #define X86_64_RELOC_GOT_LOAD 3 /* MOVQ of GOT entry */
  88. #define X86_64_RELOC_GOT 4 /* Different GOT entry */
  89. #define X86_64_RELOC_SUBTRACTOR 5 /* Subtracting two symbols */
  90. #define X86_64_RELOC_SIGNED_1 6 /* SIGNED with -1 addend */
  91. #define X86_64_RELOC_SIGNED_2 7 /* SIGNED with -2 addend */
  92. #define X86_64_RELOC_SIGNED_4 8 /* SIGNED with -4 addend */
  93. #define X86_64_RELOC_TLV 9 /* Thread local */
  94. /* Mach-O VM permission constants */
  95. #define VM_PROT_NONE (0x00)
  96. #define VM_PROT_READ (0x01)
  97. #define VM_PROT_WRITE (0x02)
  98. #define VM_PROT_EXECUTE (0x04)
  99. #define VM_PROT_DEFAULT (VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE)
  100. #define VM_PROT_ALL (VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE)
  101. /* Our internal relocation types */
  102. enum reltype {
  103. RL_ABS, /* Absolute relocation */
  104. RL_REL, /* Relative relocation */
  105. RL_TLV, /* Thread local */
  106. RL_BRANCH, /* Relative direct branch */
  107. RL_SUB, /* X86_64_RELOC_SUBTRACT */
  108. RL_GOT, /* X86_64_RELOC_GOT */
  109. RL_GOTLOAD /* X86_64_RELOC_GOT_LOAD */
  110. };
  111. #define RL_MAX_32 RL_TLV
  112. #define RL_MAX_64 RL_GOTLOAD
  113. struct macho_fmt {
  114. uint32_t ptrsize; /* Pointer size in bytes */
  115. uint32_t mh_magic; /* Which magic number to use */
  116. uint32_t cpu_type; /* Which CPU type */
  117. uint32_t lc_segment; /* Which segment load command */
  118. uint32_t header_size; /* Header size */
  119. uint32_t segcmd_size; /* Segment command size */
  120. uint32_t sectcmd_size; /* Section command size */
  121. uint32_t nlist_size; /* Nlist (symbol) size */
  122. enum reltype maxreltype; /* Maximum entry in enum reltype permitted */
  123. uint32_t reloc_abs; /* Absolute relocation type */
  124. uint32_t reloc_rel; /* Relative relocation type */
  125. uint32_t reloc_tlv; /* Thread local relocation type */
  126. bool forcesym; /* Always use "external" (symbol-relative) relocations */
  127. };
  128. static struct macho_fmt fmt;
  129. static void fwriteptr(uint64_t data, FILE * fp)
  130. {
  131. fwriteaddr(data, fmt.ptrsize, fp);
  132. }
  133. struct section {
  134. /* nasm internal data */
  135. struct section *next;
  136. struct SAA *data;
  137. int32_t index; /* Main section index */
  138. int32_t subsection; /* Current subsection index */
  139. int32_t fileindex;
  140. struct reloc *relocs;
  141. struct rbtree *syms[2]; /* All/global symbols symbols in section */
  142. int align;
  143. bool by_name; /* This section was specified by full MachO name */
  144. char namestr[34]; /* segment,section as a C string */
  145. /* data that goes into the file */
  146. char sectname[16]; /* what this section is called */
  147. char segname[16]; /* segment this section will be in */
  148. uint64_t addr; /* in-memory address (subject to alignment) */
  149. uint64_t size; /* in-memory and -file size */
  150. uint64_t offset; /* in-file offset */
  151. uint32_t pad; /* padding bytes before section */
  152. uint32_t nreloc; /* relocation entry count */
  153. uint32_t flags; /* type and attributes (masked) */
  154. uint32_t extreloc; /* external relocations */
  155. };
  156. #define SECTION_TYPE 0x000000ff /* section type mask */
  157. #define S_REGULAR (0x0) /* standard section */
  158. #define S_ZEROFILL (0x1) /* zerofill, in-memory only */
  159. #define SECTION_ATTRIBUTES_SYS 0x00ffff00 /* system setable attributes */
  160. #define S_ATTR_SOME_INSTRUCTIONS 0x00000400 /* section contains some
  161. machine instructions */
  162. #define S_ATTR_EXT_RELOC 0x00000200 /* section has external relocation entries */
  163. #define S_ATTR_LOC_RELOC 0x00000100 /* section has local relocation entries */
  164. #define S_ATTR_DEBUG 0x02000000
  165. #define S_ATTR_SELF_MODIFYING_CODE 0x04000000
  166. #define S_ATTR_LIVE_SUPPORT 0x08000000
  167. #define S_ATTR_NO_DEAD_STRIP 0x10000000 /* no dead stripping */
  168. #define S_ATTR_STRIP_STATIC_SYMS 0x20000000
  169. #define S_ATTR_NO_TOC 0x40000000
  170. #define S_ATTR_PURE_INSTRUCTIONS 0x80000000 /* section uses pure machine instructions */
  171. #define S_ATTR_DEBUG 0x02000000 /* debug section */
  172. #define S_NASM_TYPE_MASK 0x800004ff /* we consider these bits "section type" */
  173. /* fake section for absolute symbols, *not* part of the section linked list */
  174. static struct section absolute_sect;
  175. struct reloc {
  176. /* nasm internal data */
  177. struct reloc *next;
  178. /* data that goes into the file */
  179. int32_t addr; /* op's offset in section */
  180. uint32_t snum:24, /* contains symbol index if
  181. ** ext otherwise in-file
  182. ** section number */
  183. pcrel:1, /* relative relocation */
  184. length:2, /* 0=byte, 1=word, 2=int32_t, 3=int64_t */
  185. ext:1, /* external symbol referenced */
  186. type:4; /* reloc type */
  187. };
  188. #define R_ABS 0 /* absolute relocation */
  189. #define R_SCATTERED 0x80000000 /* reloc entry is scattered if
  190. ** highest bit == 1 */
  191. struct symbol {
  192. /* nasm internal data */
  193. struct rbtree symv[2]; /* All/global symbol rbtrees; "key" contains the
  194. symbol offset. */
  195. struct symbol *next; /* next symbol in the list */
  196. char *name; /* name of this symbol */
  197. int32_t initial_snum; /* symbol number used above in reloc */
  198. int32_t snum; /* true snum for reloc */
  199. /* data that goes into the file */
  200. uint32_t strx; /* string table index */
  201. uint8_t type; /* symbol type */
  202. uint8_t sect; /* NO_SECT or section number */
  203. uint16_t desc; /* for stab debugging, 0 for us */
  204. };
  205. /* symbol type bits */
  206. #define N_EXT 0x01 /* global or external symbol */
  207. #define N_PEXT 0x10 /* private external symbol */
  208. #define N_UNDF 0x0 /* undefined symbol | n_sect == */
  209. #define N_ABS 0x2 /* absolute symbol | NO_SECT */
  210. #define N_SECT 0xe /* defined symbol, n_sect holds
  211. ** section number */
  212. #define N_TYPE 0x0e /* type bit mask */
  213. #define DEFAULT_SECTION_ALIGNMENT 0 /* byte (i.e. no) alignment */
  214. /* special section number values */
  215. #define NO_SECT 0 /* no section, invalid */
  216. #define MAX_SECT 255 /* maximum number of sections */
  217. static struct section *sects, **sectstail, **sectstab;
  218. static struct symbol *syms, **symstail;
  219. static uint32_t nsyms;
  220. /* These variables are set by macho_layout_symbols() to organize
  221. the symbol table and string table in order the dynamic linker
  222. expects. They are then used in macho_write() to put out the
  223. symbols and strings in that order.
  224. The order of the symbol table is:
  225. local symbols
  226. defined external symbols (sorted by name)
  227. undefined external symbols (sorted by name)
  228. The order of the string table is:
  229. strings for external symbols
  230. strings for local symbols
  231. */
  232. static uint32_t ilocalsym = 0;
  233. static uint32_t iextdefsym = 0;
  234. static uint32_t iundefsym = 0;
  235. static uint32_t nlocalsym;
  236. static uint32_t nextdefsym;
  237. static uint32_t nundefsym;
  238. static struct symbol **extdefsyms = NULL;
  239. static struct symbol **undefsyms = NULL;
  240. static struct RAA *extsyms;
  241. static struct SAA *strs;
  242. static uint32_t strslen;
  243. /* Global file information. This should be cleaned up into either
  244. a structure or as function arguments. */
  245. static uint32_t head_ncmds = 0;
  246. static uint32_t head_sizeofcmds = 0;
  247. static uint32_t head_flags = 0;
  248. static uint64_t seg_filesize = 0;
  249. static uint64_t seg_vmsize = 0;
  250. static uint32_t seg_nsects = 0;
  251. static uint64_t rel_padcnt = 0;
  252. /*
  253. * Functions for handling fixed-length zero-padded string
  254. * fields, that may or may not be null-terminated.
  255. */
  256. /* Copy a string into a zero-padded fixed-length field */
  257. #define xstrncpy(xdst, xsrc) strncpy(xdst, xsrc, sizeof(xdst))
  258. /* Compare a fixed-length field with a string */
  259. #define xstrncmp(xdst, xsrc) strncmp(xdst, xsrc, sizeof(xdst))
  260. #define alignint32_t(x) \
  261. ALIGN(x, sizeof(int32_t)) /* align x to int32_t boundary */
  262. #define alignint64_t(x) \
  263. ALIGN(x, sizeof(int64_t)) /* align x to int64_t boundary */
  264. #define alignptr(x) \
  265. ALIGN(x, fmt.ptrsize) /* align x to output format width */
  266. static struct hash_table section_by_name;
  267. static struct RAAPTR *section_by_index;
  268. static struct section * never_null
  269. find_or_add_section(const char *segname, const char *sectname)
  270. {
  271. struct hash_insert hi;
  272. void **sp;
  273. struct section *s;
  274. char sect[34];
  275. snprintf(sect, sizeof sect, "%-16s,%-16s", segname, sectname);
  276. sp = hash_find(&section_by_name, sect, &hi);
  277. if (sp)
  278. return (struct section *)(*sp);
  279. s = nasm_zalloc(sizeof *s);
  280. xstrncpy(s->segname, segname);
  281. xstrncpy(s->sectname, sectname);
  282. xstrncpy(s->namestr, sect);
  283. hash_add(&hi, s->namestr, s);
  284. s->index = s->subsection = seg_alloc();
  285. section_by_index = raa_write_ptr(section_by_index, s->index >> 1, s);
  286. return s;
  287. }
  288. static inline bool is_new_section(const struct section *s)
  289. {
  290. return !s->data;
  291. }
  292. static struct section *get_section_by_name(const char *segname,
  293. const char *sectname)
  294. {
  295. char sect[34];
  296. void **sp;
  297. snprintf(sect, sizeof sect, "%-16s,%-16s", segname, sectname);
  298. sp = hash_find(&section_by_name, sect, NULL);
  299. return sp ? (struct section *)(*sp) : NULL;
  300. }
  301. static struct section *get_section_by_index(int32_t index)
  302. {
  303. if (index < 0 || index >= SEG_ABS || (index & 1))
  304. return NULL;
  305. return raa_read_ptr(section_by_index, index >> 1);
  306. }
  307. struct dir_list {
  308. struct dir_list *next;
  309. struct dir_list *last;
  310. const char *dir_name;
  311. uint32_t dir;
  312. };
  313. struct file_list {
  314. struct file_list *next;
  315. struct file_list *last;
  316. const char *file_name;
  317. uint32_t file;
  318. struct dir_list *dir;
  319. };
  320. struct dw_sect_list {
  321. struct SAA *psaa;
  322. int32_t section;
  323. uint32_t line;
  324. uint64_t offset;
  325. uint32_t file;
  326. struct dw_sect_list *next;
  327. struct dw_sect_list *last;
  328. };
  329. struct section_info {
  330. uint64_t size;
  331. int32_t secto;
  332. };
  333. #define DW_LN_BASE (-5)
  334. #define DW_LN_RANGE 14
  335. #define DW_OPCODE_BASE 13
  336. #define DW_MAX_LN (DW_LN_BASE + DW_LN_RANGE)
  337. #define DW_MAX_SP_OPCODE 256
  338. static struct file_list *dw_head_file = 0, *dw_cur_file = 0, **dw_last_file_next = NULL;
  339. static struct dir_list *dw_head_dir = 0, **dw_last_dir_next = NULL;
  340. static struct dw_sect_list *dw_head_sect = 0, *dw_cur_sect = 0, *dw_last_sect = 0;
  341. static uint32_t cur_line = 0, dw_num_files = 0, dw_num_dirs = 0, dw_num_sects = 0;
  342. static bool dbg_immcall = false;
  343. static const char *module_name = NULL;
  344. /*
  345. * Special section numbers which are used to define Mach-O special
  346. * symbols, which can be used with WRT to provide PIC relocation
  347. * types.
  348. */
  349. static int32_t macho_tlvp_sect;
  350. static int32_t macho_gotpcrel_sect;
  351. static void macho_init(void)
  352. {
  353. module_name = inname;
  354. sects = NULL;
  355. sectstail = &sects;
  356. /* Fake section for absolute symbols */
  357. absolute_sect.index = NO_SEG;
  358. syms = NULL;
  359. symstail = &syms;
  360. nsyms = 0;
  361. nlocalsym = 0;
  362. nextdefsym = 0;
  363. nundefsym = 0;
  364. extsyms = raa_init();
  365. strs = saa_init(1L);
  366. section_by_index = raa_init_ptr();
  367. hash_init(&section_by_name, HASH_MEDIUM);
  368. /* string table starts with a zero byte so index 0 is an empty string */
  369. saa_wbytes(strs, zero_buffer, 1);
  370. strslen = 1;
  371. /* add special symbol for TLVP */
  372. macho_tlvp_sect = seg_alloc() + 1;
  373. backend_label("..tlvp", macho_tlvp_sect, 0L);
  374. }
  375. static void sect_write(struct section *sect,
  376. const uint8_t *data, uint32_t len)
  377. {
  378. saa_wbytes(sect->data, data, len);
  379. sect->size += len;
  380. }
  381. /*
  382. * Find a suitable global symbol for a ..gotpcrel or ..tlvp reference
  383. */
  384. static struct symbol *macho_find_sym(struct section *s, uint64_t offset,
  385. bool global, bool exact)
  386. {
  387. struct rbtree *srb;
  388. srb = rb_search(s->syms[global], offset);
  389. if (!srb || (exact && srb->key != offset)) {
  390. nasm_error(ERR_NONFATAL, "unable to find a suitable%s%s symbol"
  391. " for this reference",
  392. global ? " global" : "",
  393. s == &absolute_sect ? " absolute " : "");
  394. return NULL;
  395. }
  396. return container_of(srb - global, struct symbol, symv);
  397. }
  398. static int64_t add_reloc(struct section *sect, int32_t section,
  399. int64_t offset,
  400. enum reltype reltype, int bytes)
  401. {
  402. struct reloc *r;
  403. struct section *s;
  404. int32_t fi;
  405. int64_t adjust;
  406. /* Double check this is a valid relocation type for this platform */
  407. nasm_assert(reltype <= fmt.maxreltype);
  408. /* the current end of the section will be the symbol's address for
  409. ** now, might have to be fixed by macho_fixup_relocs() later on. make
  410. ** sure we don't make the symbol scattered by setting the highest
  411. ** bit by accident */
  412. r = nasm_malloc(sizeof(struct reloc));
  413. r->addr = sect->size & ~R_SCATTERED;
  414. r->ext = 1;
  415. adjust = 0;
  416. /* match byte count 1, 2, 4, 8 to length codes 0, 1, 2, 3 respectively */
  417. r->length = ilog2_32(bytes);
  418. /* set default relocation values */
  419. r->type = fmt.reloc_abs;
  420. r->pcrel = 0;
  421. r->snum = R_ABS;
  422. s = get_section_by_index(section);
  423. fi = s ? s->fileindex : NO_SECT;
  424. /* absolute relocation */
  425. switch (reltype) {
  426. case RL_ABS:
  427. if (section == NO_SEG) {
  428. /* absolute (can this even happen?) */
  429. r->ext = 0;
  430. } else if (fi == NO_SECT) {
  431. /* external */
  432. r->snum = raa_read(extsyms, section);
  433. } else {
  434. /* local */
  435. r->ext = 0;
  436. r->snum = fi;
  437. }
  438. break;
  439. case RL_REL:
  440. case RL_BRANCH:
  441. r->type = fmt.reloc_rel;
  442. r->pcrel = 1;
  443. if (section == NO_SEG) {
  444. /* may optionally be converted below by fmt.forcesym */
  445. r->ext = 0;
  446. } else if (fi == NO_SECT) {
  447. /* external */
  448. sect->extreloc = 1;
  449. r->snum = raa_read(extsyms, section);
  450. if (reltype == RL_BRANCH)
  451. r->type = X86_64_RELOC_BRANCH;
  452. } else {
  453. /* local */
  454. r->ext = 0;
  455. r->snum = fi;
  456. if (reltype == RL_BRANCH)
  457. r->type = X86_64_RELOC_BRANCH;
  458. }
  459. break;
  460. case RL_SUB: /* obsolete */
  461. nasm_error(ERR_WARNING, "relcation with subtraction"
  462. "becomes to be obsolete");
  463. r->ext = 0;
  464. r->type = X86_64_RELOC_SUBTRACTOR;
  465. break;
  466. case RL_GOT:
  467. r->type = X86_64_RELOC_GOT;
  468. goto needsym;
  469. case RL_GOTLOAD:
  470. r->type = X86_64_RELOC_GOT_LOAD;
  471. goto needsym;
  472. case RL_TLV:
  473. r->type = fmt.reloc_tlv;
  474. goto needsym;
  475. needsym:
  476. r->pcrel = (fmt.ptrsize == 8 ? 1 : 0);
  477. if (section == NO_SEG) {
  478. nasm_error(ERR_NONFATAL, "Unsupported use of use of WRT");
  479. goto bail;
  480. } else if (fi == NO_SECT) {
  481. /* external */
  482. r->snum = raa_read(extsyms, section);
  483. } else {
  484. /* internal - GOTPCREL doesn't need to be in global */
  485. struct symbol *sym = macho_find_sym(s, offset,
  486. false, /* reltype != RL_TLV */
  487. true);
  488. if (!sym) {
  489. nasm_error(ERR_NONFATAL, "Symbol for WRT not found");
  490. goto bail;
  491. }
  492. adjust -= sym->symv[0].key;
  493. r->snum = sym->initial_snum;
  494. }
  495. break;
  496. }
  497. /*
  498. * For 64-bit Mach-O, force a symbol reference if at all possible
  499. * Allow for r->snum == R_ABS by searching absolute_sect
  500. */
  501. if (!r->ext && fmt.forcesym) {
  502. struct symbol *sym = macho_find_sym(s ? s : &absolute_sect,
  503. offset, false, false);
  504. if (sym) {
  505. adjust -= sym->symv[0].key;
  506. r->snum = sym->initial_snum;
  507. r->ext = 1;
  508. }
  509. }
  510. if (r->pcrel)
  511. adjust += ((r->ext && fmt.ptrsize == 8) ? bytes : -(int64_t)sect->size);
  512. /* NeXT as puts relocs in reversed order (address-wise) into the
  513. ** files, so we do the same, doesn't seem to make much of a
  514. ** difference either way */
  515. r->next = sect->relocs;
  516. sect->relocs = r;
  517. if (r->ext)
  518. sect->extreloc = 1;
  519. ++sect->nreloc;
  520. return adjust;
  521. bail:
  522. nasm_free(r);
  523. return 0;
  524. }
  525. static void macho_output(int32_t secto, const void *data,
  526. enum out_type type, uint64_t size,
  527. int32_t section, int32_t wrt)
  528. {
  529. struct section *s;
  530. int64_t addr, offset;
  531. uint8_t mydata[16], *p;
  532. bool is_bss;
  533. enum reltype reltype;
  534. if (secto == NO_SEG) {
  535. if (type != OUT_RESERVE)
  536. nasm_error(ERR_NONFATAL, "attempt to assemble code in "
  537. "[ABSOLUTE] space");
  538. return;
  539. }
  540. s = get_section_by_index(secto);
  541. if (!s) {
  542. nasm_error(ERR_WARNING, "attempt to assemble code in"
  543. " section %d: defaulting to `.text'", secto);
  544. s = get_section_by_name("__TEXT", "__text");
  545. /* should never happen */
  546. if (!s)
  547. nasm_panic(0, "text section not found");
  548. }
  549. /* debug code generation only for sections tagged with
  550. * instruction attribute */
  551. if (s->flags & S_ATTR_SOME_INSTRUCTIONS)
  552. {
  553. struct section_info sinfo;
  554. sinfo.size = s->size;
  555. sinfo.secto = secto;
  556. dfmt->debug_output(0, &sinfo);
  557. }
  558. is_bss = (s->flags & SECTION_TYPE) == S_ZEROFILL;
  559. if (is_bss && type != OUT_RESERVE) {
  560. nasm_error(ERR_WARNING, "attempt to initialize memory in "
  561. "BSS section: ignored");
  562. /* FIXME */
  563. nasm_error(ERR_WARNING, "section size may be negative"
  564. "with address symbols");
  565. s->size += realsize(type, size);
  566. return;
  567. }
  568. memset(mydata, 0, sizeof(mydata));
  569. switch (type) {
  570. case OUT_RESERVE:
  571. if (!is_bss) {
  572. nasm_error(ERR_WARNING, "uninitialized space declared in"
  573. " %s,%s section: zeroing", s->segname, s->sectname);
  574. sect_write(s, NULL, size);
  575. } else
  576. s->size += size;
  577. break;
  578. case OUT_RAWDATA:
  579. if (section != NO_SEG)
  580. nasm_panic(0, "OUT_RAWDATA with other than NO_SEG");
  581. sect_write(s, data, size);
  582. break;
  583. case OUT_ADDRESS:
  584. {
  585. int asize = abs((int)size);
  586. addr = *(int64_t *)data;
  587. if (section != NO_SEG) {
  588. if (section % 2) {
  589. nasm_error(ERR_NONFATAL, "Mach-O format does not support"
  590. " section base references");
  591. } else if (wrt == NO_SEG) {
  592. if (fmt.ptrsize == 8 && asize != 8) {
  593. nasm_error(ERR_NONFATAL,
  594. "Mach-O 64-bit format does not support"
  595. " 32-bit absolute addresses");
  596. } else {
  597. addr += add_reloc(s, section, addr, RL_ABS, asize);
  598. }
  599. } else if (wrt == macho_tlvp_sect && fmt.ptrsize != 8 &&
  600. asize == (int) fmt.ptrsize) {
  601. addr += add_reloc(s, section, addr, RL_TLV, asize);
  602. } else {
  603. nasm_error(ERR_NONFATAL, "Mach-O format does not support"
  604. " this use of WRT");
  605. }
  606. }
  607. p = mydata;
  608. WRITEADDR(p, addr, asize);
  609. sect_write(s, mydata, asize);
  610. break;
  611. }
  612. case OUT_REL1ADR:
  613. case OUT_REL2ADR:
  614. p = mydata;
  615. offset = *(int64_t *)data;
  616. addr = offset - size;
  617. if (section != NO_SEG && section % 2) {
  618. nasm_error(ERR_NONFATAL, "Mach-O format does not support"
  619. " section base references");
  620. } else if (fmt.ptrsize == 8) {
  621. nasm_error(ERR_NONFATAL, "Unsupported non-32-bit"
  622. " Macho-O relocation [2]");
  623. } else if (wrt != NO_SEG) {
  624. nasm_error(ERR_NONFATAL, "Mach-O format does not support"
  625. " this use of WRT");
  626. wrt = NO_SEG; /* we can at least _try_ to continue */
  627. } else {
  628. addr += add_reloc(s, section, addr+size, RL_REL,
  629. type == OUT_REL1ADR ? 1 : 2);
  630. }
  631. WRITESHORT(p, addr);
  632. sect_write(s, mydata, type == OUT_REL1ADR ? 1 : 2);
  633. break;
  634. case OUT_REL4ADR:
  635. case OUT_REL8ADR:
  636. p = mydata;
  637. offset = *(int64_t *)data;
  638. addr = offset - size;
  639. reltype = RL_REL;
  640. if (section != NO_SEG && section % 2) {
  641. nasm_error(ERR_NONFATAL, "Mach-O format does not support"
  642. " section base references");
  643. } else if (wrt == NO_SEG) {
  644. if (fmt.ptrsize == 8 &&
  645. (s->flags & S_ATTR_SOME_INSTRUCTIONS)) {
  646. uint8_t opcode[2];
  647. opcode[0] = opcode[1] = 0;
  648. /* HACK: Retrieve instruction opcode */
  649. if (likely(s->data->datalen >= 2)) {
  650. saa_fread(s->data, s->data->datalen-2, opcode, 2);
  651. } else if (s->data->datalen == 1) {
  652. saa_fread(s->data, 0, opcode+1, 1);
  653. }
  654. if ((opcode[0] != 0x0f && (opcode[1] & 0xfe) == 0xe8) ||
  655. (opcode[0] == 0x0f && (opcode[1] & 0xf0) == 0x80)) {
  656. /* Direct call, jmp, or jcc */
  657. reltype = RL_BRANCH;
  658. }
  659. }
  660. } else if (wrt == macho_gotpcrel_sect) {
  661. reltype = RL_GOT;
  662. if ((s->flags & S_ATTR_SOME_INSTRUCTIONS) &&
  663. s->data->datalen >= 3) {
  664. uint8_t gotload[3];
  665. /* HACK: Retrieve instruction opcode */
  666. saa_fread(s->data, s->data->datalen-3, gotload, 3);
  667. if ((gotload[0] & 0xf8) == 0x48 &&
  668. gotload[1] == 0x8b &&
  669. (gotload[2] & 0307) == 0005) {
  670. /* movq <reg>,[rel sym wrt ..gotpcrel] */
  671. reltype = RL_GOTLOAD;
  672. }
  673. }
  674. } else if (wrt == macho_tlvp_sect && fmt.ptrsize == 8) {
  675. reltype = RL_TLV;
  676. } else {
  677. nasm_error(ERR_NONFATAL, "Mach-O format does not support"
  678. " this use of WRT");
  679. /* continue with RL_REL */
  680. }
  681. addr += add_reloc(s, section, offset, reltype,
  682. type == OUT_REL4ADR ? 4 : 8);
  683. WRITELONG(p, addr);
  684. sect_write(s, mydata, type == OUT_REL4ADR ? 4 : 8);
  685. break;
  686. default:
  687. nasm_error(ERR_NONFATAL, "Unrepresentable relocation in Mach-O");
  688. break;
  689. }
  690. }
  691. /* Translation table from traditional Unix section names to Mach-O */
  692. static const struct sectmap {
  693. const char *nasmsect;
  694. const char *segname;
  695. const char *sectname;
  696. const uint32_t flags;
  697. } sectmap[] = {
  698. {".text", "__TEXT", "__text",
  699. S_REGULAR|S_ATTR_SOME_INSTRUCTIONS|S_ATTR_PURE_INSTRUCTIONS},
  700. {".data", "__DATA", "__data", S_REGULAR},
  701. {".rodata", "__DATA", "__const", S_REGULAR},
  702. {".bss", "__DATA", "__bss", S_ZEROFILL},
  703. {".debug_abbrev", "__DWARF", "__debug_abbrev", S_ATTR_DEBUG},
  704. {".debug_info", "__DWARF", "__debug_info", S_ATTR_DEBUG},
  705. {".debug_line", "__DWARF", "__debug_line", S_ATTR_DEBUG},
  706. {".debug_str", "__DWARF", "__debug_str", S_ATTR_DEBUG},
  707. {NULL, NULL, NULL, 0}
  708. };
  709. #define NO_TYPE S_NASM_TYPE_MASK
  710. /* Section type or attribute directives */
  711. static const struct sect_attribs {
  712. const char *name;
  713. uint32_t flags;
  714. } sect_attribs[] = {
  715. { "data", S_REGULAR },
  716. { "code", S_REGULAR|S_ATTR_SOME_INSTRUCTIONS|S_ATTR_PURE_INSTRUCTIONS },
  717. { "mixed", S_REGULAR|S_ATTR_SOME_INSTRUCTIONS },
  718. { "bss", S_ZEROFILL },
  719. { "zerofill", S_ZEROFILL },
  720. { "no_dead_strip", NO_TYPE|S_ATTR_NO_DEAD_STRIP },
  721. { "live_support", NO_TYPE|S_ATTR_LIVE_SUPPORT },
  722. { "strip_static_syms", NO_TYPE|S_ATTR_STRIP_STATIC_SYMS },
  723. { "debug", NO_TYPE|S_ATTR_DEBUG },
  724. { NULL, 0 }
  725. };
  726. static int32_t macho_section(char *name, int pass, int *bits)
  727. {
  728. char *sectionAttributes;
  729. const struct sectmap *sm;
  730. struct section *s;
  731. const char *section, *segment;
  732. uint32_t flags;
  733. const struct sect_attribs *sa;
  734. char *currentAttribute;
  735. char *comma;
  736. bool new_seg;
  737. (void)pass;
  738. /* Default to the appropriate number of bits. */
  739. if (!name) {
  740. *bits = fmt.ptrsize << 3;
  741. name = ".text";
  742. sectionAttributes = NULL;
  743. } else {
  744. sectionAttributes = name;
  745. name = nasm_strsep(&sectionAttributes, " \t");
  746. }
  747. section = segment = NULL;
  748. flags = 0;
  749. comma = strchr(name, ',');
  750. if (comma) {
  751. int len;
  752. *comma = '\0';
  753. segment = name;
  754. section = comma+1;
  755. len = strlen(segment);
  756. if (len == 0) {
  757. nasm_error(ERR_NONFATAL, "empty segment name\n");
  758. } else if (len > 16) {
  759. nasm_error(ERR_NONFATAL, "segment name %s too long\n", segment);
  760. }
  761. len = strlen(section);
  762. if (len == 0) {
  763. nasm_error(ERR_NONFATAL, "empty section name\n");
  764. } else if (len > 16) {
  765. nasm_error(ERR_NONFATAL, "section name %s too long\n", section);
  766. }
  767. if (!strcmp(section, "__text")) {
  768. flags = S_REGULAR | S_ATTR_SOME_INSTRUCTIONS |
  769. S_ATTR_PURE_INSTRUCTIONS;
  770. } else if (!strcmp(section, "__bss")) {
  771. flags = S_ZEROFILL;
  772. } else {
  773. flags = S_REGULAR;
  774. }
  775. } else {
  776. for (sm = sectmap; sm->nasmsect != NULL; ++sm) {
  777. /* make lookup into section name translation table */
  778. if (!strcmp(name, sm->nasmsect)) {
  779. segment = sm->segname;
  780. section = sm->sectname;
  781. flags = sm->flags;
  782. goto found;
  783. }
  784. }
  785. nasm_error(ERR_NONFATAL, "unknown section name\n");
  786. return NO_SEG;
  787. }
  788. found:
  789. /* try to find section with that name, or create it */
  790. s = find_or_add_section(segment, section);
  791. new_seg = is_new_section(s);
  792. /* initialize it if it is a brand new section */
  793. if (new_seg) {
  794. *sectstail = s;
  795. sectstail = &s->next;
  796. s->data = saa_init(1L);
  797. s->fileindex = ++seg_nsects;
  798. s->align = -1;
  799. s->pad = -1;
  800. s->offset = -1;
  801. s->by_name = false;
  802. s->size = 0;
  803. s->nreloc = 0;
  804. s->flags = flags;
  805. }
  806. if (comma)
  807. *comma = ','; /* Restore comma */
  808. s->by_name = s->by_name || comma; /* Was specified by name */
  809. flags = NO_TYPE;
  810. while (sectionAttributes &&
  811. (currentAttribute = nasm_strsep(&sectionAttributes, " \t"))) {
  812. if (!*currentAttribute)
  813. continue;
  814. if (!nasm_strnicmp("align=", currentAttribute, 6)) {
  815. char *end;
  816. int newAlignment, value;
  817. value = strtoul(currentAttribute + 6, (char**)&end, 0);
  818. newAlignment = alignlog2_32(value);
  819. if (0 != *end) {
  820. nasm_error(ERR_NONFATAL,
  821. "unknown or missing alignment value \"%s\" "
  822. "specified for section \"%s\"",
  823. currentAttribute + 6,
  824. name);
  825. } else if (0 > newAlignment) {
  826. nasm_error(ERR_NONFATAL,
  827. "alignment of %d (for section \"%s\") is not "
  828. "a power of two",
  829. value,
  830. name);
  831. }
  832. if (s->align < newAlignment)
  833. s->align = newAlignment;
  834. } else {
  835. for (sa = sect_attribs; sa->name; sa++) {
  836. if (!nasm_stricmp(sa->name, currentAttribute)) {
  837. if ((sa->flags & S_NASM_TYPE_MASK) != NO_TYPE) {
  838. flags = (flags & ~S_NASM_TYPE_MASK)
  839. | (sa->flags & S_NASM_TYPE_MASK);
  840. }
  841. flags |= sa->flags & ~S_NASM_TYPE_MASK;
  842. break;
  843. }
  844. }
  845. if (!sa->name) {
  846. nasm_error(ERR_NONFATAL,
  847. "unknown section attribute %s for section %s",
  848. currentAttribute, name);
  849. }
  850. }
  851. }
  852. if ((flags & S_NASM_TYPE_MASK) != NO_TYPE) {
  853. if (!new_seg && ((s->flags ^ flags) & S_NASM_TYPE_MASK)) {
  854. nasm_error(ERR_NONFATAL,
  855. "inconsistent section attributes for section %s\n",
  856. name);
  857. } else {
  858. s->flags = (s->flags & ~S_NASM_TYPE_MASK) | flags;
  859. }
  860. } else {
  861. s->flags |= flags & ~S_NASM_TYPE_MASK;
  862. }
  863. return s->subsection;
  864. }
  865. static int32_t macho_herelabel(const char *name, enum label_type type,
  866. int32_t section, int32_t *subsection,
  867. bool *copyoffset)
  868. {
  869. struct section *s;
  870. int32_t subsec;
  871. (void)name;
  872. if (!(head_flags & MH_SUBSECTIONS_VIA_SYMBOLS))
  873. return section;
  874. /* No subsection only for local labels */
  875. if (type == LBL_LOCAL)
  876. return section;
  877. s = get_section_by_index(section);
  878. if (!s)
  879. return section;
  880. subsec = *subsection;
  881. if (subsec == NO_SEG) {
  882. /* Allocate a new subsection index */
  883. subsec = *subsection = seg_alloc();
  884. section_by_index = raa_write_ptr(section_by_index, subsec >> 1, s);
  885. }
  886. s->subsection = subsec;
  887. *copyoffset = true; /* Maintain previous offset */
  888. return subsec;
  889. }
  890. static void macho_symdef(char *name, int32_t section, int64_t offset,
  891. int is_global, char *special)
  892. {
  893. struct symbol *sym;
  894. struct section *s;
  895. bool special_used = false;
  896. #if defined(DEBUG) && DEBUG>2
  897. nasm_error(ERR_DEBUG,
  898. " macho_symdef: %s, pass0=%d, passn=%"PRId64", sec=%"PRIx32", off=%"PRIx64", is_global=%d, %s\n",
  899. name, pass0, passn, section, offset, is_global, special);
  900. #endif
  901. if (is_global == 3) {
  902. if (special) {
  903. int n = strcspn(special, " \t");
  904. if (!nasm_strnicmp(special, "private_extern", n)) {
  905. for (sym = syms; sym != NULL; sym = sym->next) {
  906. if (!strcmp(name, sym->name)) {
  907. if (sym->type & N_PEXT)
  908. return; /* nothing to be done */
  909. else
  910. break;
  911. }
  912. }
  913. }
  914. }
  915. nasm_error(ERR_NONFATAL, "The Mach-O format does not "
  916. "(yet) support forward reference fixups.");
  917. return;
  918. }
  919. if (name[0] == '.' && name[1] == '.' && name[2] != '@') {
  920. /*
  921. * This is a NASM special symbol. We never allow it into
  922. * the Macho-O symbol table, even if it's a valid one. If it
  923. * _isn't_ a valid one, we should barf immediately.
  924. */
  925. if (strcmp(name, "..gotpcrel") && strcmp(name, "..tlvp"))
  926. nasm_error(ERR_NONFATAL, "unrecognized special symbol `%s'", name);
  927. return;
  928. }
  929. sym = *symstail = nasm_zalloc(sizeof(struct symbol));
  930. sym->next = NULL;
  931. symstail = &sym->next;
  932. sym->name = name;
  933. sym->strx = strslen;
  934. sym->type = 0;
  935. sym->desc = 0;
  936. sym->symv[0].key = offset;
  937. sym->symv[1].key = offset;
  938. sym->initial_snum = -1;
  939. /* external and common symbols get N_EXT */
  940. if (is_global != 0) {
  941. sym->type |= N_EXT;
  942. }
  943. if (is_global == 1) {
  944. /* check special to see if the global symbol shall be marked as private external: N_PEXT */
  945. if (special) {
  946. int n = strcspn(special, " \t");
  947. if (!nasm_strnicmp(special, "private_extern", n))
  948. sym->type |= N_PEXT;
  949. else
  950. nasm_error(ERR_NONFATAL, "unrecognised symbol type `%.*s'", n, special);
  951. }
  952. special_used = true;
  953. }
  954. /* track the initially allocated symbol number for use in future fix-ups */
  955. sym->initial_snum = nsyms;
  956. if (section == NO_SEG) {
  957. /* symbols in no section get absolute */
  958. sym->type |= N_ABS;
  959. sym->sect = NO_SECT;
  960. s = &absolute_sect;
  961. } else {
  962. s = get_section_by_index(section);
  963. sym->type |= N_SECT;
  964. /* get the in-file index of the section the symbol was defined in */
  965. sym->sect = s ? s->fileindex : NO_SECT;
  966. if (!s) {
  967. /* remember symbol number of references to external
  968. ** symbols, this works because every external symbol gets
  969. ** its own section number allocated internally by nasm and
  970. ** can so be used as a key */
  971. extsyms = raa_write(extsyms, section, nsyms);
  972. switch (is_global) {
  973. case 1:
  974. case 2:
  975. /* there isn't actually a difference between global
  976. ** and common symbols, both even have their size in
  977. ** sym->symv[0].key */
  978. sym->type = N_EXT;
  979. break;
  980. default:
  981. /* give an error on unfound section if it's not an
  982. ** external or common symbol (assemble_file() does a
  983. ** seg_alloc() on every call for them) */
  984. nasm_panic(0, "in-file index for section %d not found, is_global = %d", section, is_global);
  985. break;
  986. }
  987. }
  988. }
  989. if (s) {
  990. s->syms[0] = rb_insert(s->syms[0], &sym->symv[0]);
  991. if (is_global)
  992. s->syms[1] = rb_insert(s->syms[1], &sym->symv[1]);
  993. }
  994. ++nsyms;
  995. if (special && !special_used)
  996. nasm_error(ERR_NONFATAL, "no special symbol features supported here");
  997. }
  998. static void macho_sectalign(int32_t seg, unsigned int value)
  999. {
  1000. struct section *s;
  1001. int align;
  1002. nasm_assert(!(seg & 1));
  1003. s = get_section_by_index(seg);
  1004. if (!s || !is_power2(value))
  1005. return;
  1006. align = alignlog2_32(value);
  1007. if (s->align < align)
  1008. s->align = align;
  1009. }
  1010. static int32_t macho_segbase(int32_t section)
  1011. {
  1012. return section;
  1013. }
  1014. extern macros_t macho_stdmac[];
  1015. /* Comparison function for qsort symbol layout. */
  1016. static int layout_compare (const struct symbol **s1,
  1017. const struct symbol **s2)
  1018. {
  1019. return (strcmp ((*s1)->name, (*s2)->name));
  1020. }
  1021. /* The native assembler does a few things in a similar function
  1022. * Remove temporary labels
  1023. * Sort symbols according to local, external, undefined (by name)
  1024. * Order the string table
  1025. We do not remove temporary labels right now.
  1026. numsyms is the total number of symbols we have. strtabsize is the
  1027. number entries in the string table. */
  1028. static void macho_layout_symbols (uint32_t *numsyms,
  1029. uint32_t *strtabsize)
  1030. {
  1031. struct symbol *sym, **symp;
  1032. uint32_t i,j;
  1033. *numsyms = 0;
  1034. *strtabsize = sizeof (char);
  1035. symp = &syms;
  1036. while ((sym = *symp)) {
  1037. /* Undefined symbols are now external. */
  1038. if (sym->type == N_UNDF)
  1039. sym->type |= N_EXT;
  1040. if ((sym->type & N_EXT) == 0) {
  1041. sym->snum = *numsyms;
  1042. *numsyms = *numsyms + 1;
  1043. nlocalsym++;
  1044. }
  1045. else {
  1046. if ((sym->type & N_TYPE) != N_UNDF) {
  1047. nextdefsym++;
  1048. } else {
  1049. nundefsym++;
  1050. }
  1051. /* If we handle debug info we'll want
  1052. to check for it here instead of just
  1053. adding the symbol to the string table. */
  1054. sym->strx = *strtabsize;
  1055. saa_wbytes (strs, sym->name, (int32_t)(strlen(sym->name) + 1));
  1056. *strtabsize += strlen(sym->name) + 1;
  1057. }
  1058. symp = &(sym->next);
  1059. }
  1060. /* Next, sort the symbols. Most of this code is a direct translation from
  1061. the Apple cctools symbol layout. We need to keep compatibility with that. */
  1062. /* Set the indexes for symbol groups into the symbol table */
  1063. ilocalsym = 0;
  1064. iextdefsym = nlocalsym;
  1065. iundefsym = nlocalsym + nextdefsym;
  1066. /* allocate arrays for sorting externals by name */
  1067. extdefsyms = nasm_malloc(nextdefsym * sizeof(struct symbol *));
  1068. undefsyms = nasm_malloc(nundefsym * sizeof(struct symbol *));
  1069. i = 0;
  1070. j = 0;
  1071. symp = &syms;
  1072. while ((sym = *symp)) {
  1073. if((sym->type & N_EXT) == 0) {
  1074. sym->strx = *strtabsize;
  1075. saa_wbytes (strs, sym->name, (int32_t)(strlen (sym->name) + 1));
  1076. *strtabsize += strlen(sym->name) + 1;
  1077. }
  1078. else {
  1079. if ((sym->type & N_TYPE) != N_UNDF) {
  1080. extdefsyms[i++] = sym;
  1081. } else {
  1082. undefsyms[j++] = sym;
  1083. }
  1084. }
  1085. symp = &(sym->next);
  1086. }
  1087. qsort(extdefsyms, nextdefsym, sizeof(struct symbol *),
  1088. (int (*)(const void *, const void *))layout_compare);
  1089. qsort(undefsyms, nundefsym, sizeof(struct symbol *),
  1090. (int (*)(const void *, const void *))layout_compare);
  1091. for(i = 0; i < nextdefsym; i++) {
  1092. extdefsyms[i]->snum = *numsyms;
  1093. *numsyms += 1;
  1094. }
  1095. for(j = 0; j < nundefsym; j++) {
  1096. undefsyms[j]->snum = *numsyms;
  1097. *numsyms += 1;
  1098. }
  1099. }
  1100. /* Calculate some values we'll need for writing later. */
  1101. static void macho_calculate_sizes (void)
  1102. {
  1103. struct section *s;
  1104. int fi;
  1105. /* count sections and calculate in-memory and in-file offsets */
  1106. for (s = sects; s != NULL; s = s->next) {
  1107. uint64_t newaddr;
  1108. /* recalculate segment address based on alignment and vm size */
  1109. s->addr = seg_vmsize;
  1110. /* we need section alignment to calculate final section address */
  1111. if (s->align == -1)
  1112. s->align = DEFAULT_SECTION_ALIGNMENT;
  1113. newaddr = ALIGN(s->addr, UINT64_C(1) << s->align);
  1114. s->addr = newaddr;
  1115. seg_vmsize = newaddr + s->size;
  1116. /* zerofill sections aren't actually written to the file */
  1117. if ((s->flags & SECTION_TYPE) != S_ZEROFILL) {
  1118. /*
  1119. * LLVM/Xcode as always aligns the section data to 4
  1120. * bytes; there is a comment in the LLVM source code that
  1121. * perhaps aligning to pointer size would be better.
  1122. */
  1123. s->pad = ALIGN(seg_filesize, 4) - seg_filesize;
  1124. s->offset = seg_filesize + s->pad;
  1125. seg_filesize += s->size + s->pad;
  1126. /* filesize and vmsize needs to be aligned */
  1127. seg_vmsize += s->pad;
  1128. }
  1129. }
  1130. /* calculate size of all headers, load commands and sections to
  1131. ** get a pointer to the start of all the raw data */
  1132. if (seg_nsects > 0) {
  1133. ++head_ncmds;
  1134. head_sizeofcmds += fmt.segcmd_size + seg_nsects * fmt.sectcmd_size;
  1135. }
  1136. if (nsyms > 0) {
  1137. ++head_ncmds;
  1138. head_sizeofcmds += MACHO_SYMCMD_SIZE;
  1139. }
  1140. if (seg_nsects > MAX_SECT) {
  1141. nasm_fatal(0, "MachO output is limited to %d sections\n",
  1142. MAX_SECT);
  1143. }
  1144. /* Create a table of sections by file index to avoid linear search */
  1145. sectstab = nasm_malloc((seg_nsects + 1) * sizeof(*sectstab));
  1146. sectstab[NO_SECT] = &absolute_sect;
  1147. for (s = sects, fi = 1; s != NULL; s = s->next, fi++)
  1148. sectstab[fi] = s;
  1149. }
  1150. /* Write out the header information for the file. */
  1151. static void macho_write_header (void)
  1152. {
  1153. fwriteint32_t(fmt.mh_magic, ofile); /* magic */
  1154. fwriteint32_t(fmt.cpu_type, ofile); /* CPU type */
  1155. fwriteint32_t(CPU_SUBTYPE_I386_ALL, ofile); /* CPU subtype */
  1156. fwriteint32_t(MH_OBJECT, ofile); /* Mach-O file type */
  1157. fwriteint32_t(head_ncmds, ofile); /* number of load commands */
  1158. fwriteint32_t(head_sizeofcmds, ofile); /* size of load commands */
  1159. fwriteint32_t(head_flags, ofile); /* flags, if any */
  1160. fwritezero(fmt.header_size - 7*4, ofile); /* reserved fields */
  1161. }
  1162. /* Write out the segment load command at offset. */
  1163. static uint32_t macho_write_segment (uint64_t offset)
  1164. {
  1165. uint64_t rel_base = alignptr(offset + seg_filesize);
  1166. uint32_t s_reloff = 0;
  1167. struct section *s;
  1168. fwriteint32_t(fmt.lc_segment, ofile); /* cmd == LC_SEGMENT_64 */
  1169. /* size of load command including section load commands */
  1170. fwriteint32_t(fmt.segcmd_size + seg_nsects * fmt.sectcmd_size,
  1171. ofile);
  1172. /* in an MH_OBJECT file all sections are in one unnamed (name
  1173. ** all zeros) segment */
  1174. fwritezero(16, ofile);
  1175. fwriteptr(0, ofile); /* in-memory offset */
  1176. fwriteptr(seg_vmsize, ofile); /* in-memory size */
  1177. fwriteptr(offset, ofile); /* in-file offset to data */
  1178. fwriteptr(seg_filesize, ofile); /* in-file size */
  1179. fwriteint32_t(VM_PROT_DEFAULT, ofile); /* maximum vm protection */
  1180. fwriteint32_t(VM_PROT_DEFAULT, ofile); /* initial vm protection */
  1181. fwriteint32_t(seg_nsects, ofile); /* number of sections */
  1182. fwriteint32_t(0, ofile); /* no flags */
  1183. /* emit section headers */
  1184. for (s = sects; s != NULL; s = s->next) {
  1185. if (s->nreloc) {
  1186. nasm_assert((s->flags & SECTION_TYPE) != S_ZEROFILL);
  1187. s->flags |= S_ATTR_LOC_RELOC;
  1188. if (s->extreloc)
  1189. s->flags |= S_ATTR_EXT_RELOC;
  1190. } else if (!xstrncmp(s->segname, "__DATA") &&
  1191. !xstrncmp(s->sectname, "__const") &&
  1192. !s->by_name &&
  1193. !get_section_by_name("__TEXT", "__const")) {
  1194. /*
  1195. * The MachO equivalent to .rodata can be either
  1196. * __DATA,__const or __TEXT,__const; the latter only if
  1197. * there are no relocations. However, when mixed it is
  1198. * better to specify the segments explicitly.
  1199. */
  1200. xstrncpy(s->segname, "__TEXT");
  1201. }
  1202. nasm_write(s->sectname, sizeof(s->sectname), ofile);
  1203. nasm_write(s->segname, sizeof(s->segname), ofile);
  1204. fwriteptr(s->addr, ofile);
  1205. fwriteptr(s->size, ofile);
  1206. /* dummy data for zerofill sections or proper values */
  1207. if ((s->flags & SECTION_TYPE) != S_ZEROFILL) {
  1208. nasm_assert(s->pad != (uint32_t)-1);
  1209. offset += s->pad;
  1210. fwriteint32_t(offset, ofile);
  1211. offset += s->size;
  1212. /* Write out section alignment, as a power of two.
  1213. e.g. 32-bit word alignment would be 2 (2^2 = 4). */
  1214. fwriteint32_t(s->align, ofile);
  1215. /* To be compatible with cctools as we emit
  1216. a zero reloff if we have no relocations. */
  1217. fwriteint32_t(s->nreloc ? rel_base + s_reloff : 0, ofile);
  1218. fwriteint32_t(s->nreloc, ofile);
  1219. s_reloff += s->nreloc * MACHO_RELINFO_SIZE;
  1220. } else {
  1221. fwriteint32_t(0, ofile);
  1222. fwriteint32_t(s->align, ofile);
  1223. fwriteint32_t(0, ofile);
  1224. fwriteint32_t(0, ofile);
  1225. }
  1226. fwriteint32_t(s->flags, ofile); /* flags */
  1227. fwriteint32_t(0, ofile); /* reserved */
  1228. fwriteptr(0, ofile); /* reserved */
  1229. }
  1230. rel_padcnt = rel_base - offset;
  1231. offset = rel_base + s_reloff;
  1232. return offset;
  1233. }
  1234. /* For a given chain of relocs r, write out the entire relocation
  1235. chain to the object file. */
  1236. static void macho_write_relocs (struct reloc *r)
  1237. {
  1238. while (r) {
  1239. uint32_t word2;
  1240. fwriteint32_t(r->addr, ofile); /* reloc offset */
  1241. word2 = r->snum;
  1242. word2 |= r->pcrel << 24;
  1243. word2 |= r->length << 25;
  1244. word2 |= r->ext << 27;
  1245. word2 |= r->type << 28;
  1246. fwriteint32_t(word2, ofile); /* reloc data */
  1247. r = r->next;
  1248. }
  1249. }
  1250. /* Write out the section data. */
  1251. static void macho_write_section (void)
  1252. {
  1253. struct section *s;
  1254. struct reloc *r;
  1255. uint8_t *p;
  1256. int32_t len;
  1257. int64_t l;
  1258. union offset {
  1259. uint64_t val;
  1260. uint8_t buf[8];
  1261. } blk;
  1262. for (s = sects; s != NULL; s = s->next) {
  1263. if ((s->flags & SECTION_TYPE) == S_ZEROFILL)
  1264. continue;
  1265. /* Like a.out Mach-O references things in the data or bss
  1266. * sections by addresses which are actually relative to the
  1267. * start of the _text_ section, in the _file_. See outaout.c
  1268. * for more information. */
  1269. saa_rewind(s->data);
  1270. for (r = s->relocs; r != NULL; r = r->next) {
  1271. len = (uint32_t)1 << r->length;
  1272. if (len > 4) /* Can this ever be an issue?! */
  1273. len = 8;
  1274. blk.val = 0;
  1275. saa_fread(s->data, r->addr, blk.buf, len);
  1276. /* get offset based on relocation type */
  1277. #ifdef WORDS_LITTLEENDIAN
  1278. l = blk.val;
  1279. #else
  1280. l = blk.buf[0];
  1281. l += ((int64_t)blk.buf[1]) << 8;
  1282. l += ((int64_t)blk.buf[2]) << 16;
  1283. l += ((int64_t)blk.buf[3]) << 24;
  1284. l += ((int64_t)blk.buf[4]) << 32;
  1285. l += ((int64_t)blk.buf[5]) << 40;
  1286. l += ((int64_t)blk.buf[6]) << 48;
  1287. l += ((int64_t)blk.buf[7]) << 56;
  1288. #endif
  1289. /* If the relocation is internal add to the current section
  1290. offset. Otherwise the only value we need is the symbol
  1291. offset which we already have. The linker takes care
  1292. of the rest of the address. */
  1293. if (!r->ext) {
  1294. /* generate final address by section address and offset */
  1295. nasm_assert(r->snum <= seg_nsects);
  1296. l += sectstab[r->snum]->addr;
  1297. if (r->pcrel)
  1298. l -= s->addr;
  1299. } else if (r->pcrel && r->type == GENERIC_RELOC_VANILLA) {
  1300. l -= s->addr;
  1301. }
  1302. /* write new offset back */
  1303. p = blk.buf;
  1304. WRITEDLONG(p, l);
  1305. saa_fwrite(s->data, r->addr, blk.buf, len);
  1306. }
  1307. /* dump the section data to file */
  1308. fwritezero(s->pad, ofile);
  1309. saa_fpwrite(s->data, ofile);
  1310. }
  1311. /* pad last section up to reloc entries on pointer boundary */
  1312. fwritezero(rel_padcnt, ofile);
  1313. /* emit relocation entries */
  1314. for (s = sects; s != NULL; s = s->next)
  1315. macho_write_relocs (s->relocs);
  1316. }
  1317. /* Write out the symbol table. We should already have sorted this
  1318. before now. */
  1319. static void macho_write_symtab (void)
  1320. {
  1321. struct symbol *sym;
  1322. uint64_t i;
  1323. /* we don't need to pad here since MACHO_RELINFO_SIZE == 8 */
  1324. for (sym = syms; sym != NULL; sym = sym->next) {
  1325. if ((sym->type & N_EXT) == 0) {
  1326. fwriteint32_t(sym->strx, ofile); /* string table entry number */
  1327. nasm_write(&sym->type, 1, ofile); /* symbol type */
  1328. nasm_write(&sym->sect, 1, ofile); /* section */
  1329. fwriteint16_t(sym->desc, ofile); /* description */
  1330. /* Fix up the symbol value now that we know the final section
  1331. sizes. */
  1332. if (((sym->type & N_TYPE) == N_SECT) && (sym->sect != NO_SECT)) {
  1333. nasm_assert(sym->sect <= seg_nsects);
  1334. sym->symv[0].key += sectstab[sym->sect]->addr;
  1335. }
  1336. fwriteptr(sym->symv[0].key, ofile); /* value (i.e. offset) */
  1337. }
  1338. }
  1339. for (i = 0; i < nextdefsym; i++) {
  1340. sym = extdefsyms[i];
  1341. fwriteint32_t(sym->strx, ofile);
  1342. nasm_write(&sym->type, 1, ofile); /* symbol type */
  1343. nasm_write(&sym->sect, 1, ofile); /* section */
  1344. fwriteint16_t(sym->desc, ofile); /* description */
  1345. /* Fix up the symbol value now that we know the final section
  1346. sizes. */
  1347. if (((sym->type & N_TYPE) == N_SECT) && (sym->sect != NO_SECT)) {
  1348. nasm_assert(sym->sect <= seg_nsects);
  1349. sym->symv[0].key += sectstab[sym->sect]->addr;
  1350. }
  1351. fwriteptr(sym->symv[0].key, ofile); /* value (i.e. offset) */
  1352. }
  1353. for (i = 0; i < nundefsym; i++) {
  1354. sym = undefsyms[i];
  1355. fwriteint32_t(sym->strx, ofile);
  1356. nasm_write(&sym->type, 1, ofile); /* symbol type */
  1357. nasm_write(&sym->sect, 1, ofile); /* section */
  1358. fwriteint16_t(sym->desc, ofile); /* description */
  1359. /* Fix up the symbol value now that we know the final section
  1360. sizes. */
  1361. if (((sym->type & N_TYPE) == N_SECT) && (sym->sect != NO_SECT)) {
  1362. nasm_assert(sym->sect <= seg_nsects);
  1363. sym->symv[0].key += sectstab[sym->sect]->addr;
  1364. }
  1365. fwriteptr(sym->symv[0].key, ofile); /* value (i.e. offset) */
  1366. }
  1367. }
  1368. /* Fixup the snum in the relocation entries, we should be
  1369. doing this only for externally referenced symbols. */
  1370. static void macho_fixup_relocs (struct reloc *r)
  1371. {
  1372. struct symbol *sym;
  1373. while (r != NULL) {
  1374. if (r->ext) {
  1375. for (sym = syms; sym != NULL; sym = sym->next) {
  1376. if (sym->initial_snum == r->snum) {
  1377. r->snum = sym->snum;
  1378. break;
  1379. }
  1380. }
  1381. }
  1382. r = r->next;
  1383. }
  1384. }
  1385. /* Write out the object file. */
  1386. static void macho_write (void)
  1387. {
  1388. uint64_t offset = 0;
  1389. /* mach-o object file structure:
  1390. **
  1391. ** mach header
  1392. ** uint32_t magic
  1393. ** int cpu type
  1394. ** int cpu subtype
  1395. ** uint32_t mach file type
  1396. ** uint32_t number of load commands
  1397. ** uint32_t size of all load commands
  1398. ** (includes section struct size of segment command)
  1399. ** uint32_t flags
  1400. **
  1401. ** segment command
  1402. ** uint32_t command type == LC_SEGMENT[_64]
  1403. ** uint32_t size of load command
  1404. ** (including section load commands)
  1405. ** char[16] segment name
  1406. ** pointer in-memory offset
  1407. ** pointer in-memory size
  1408. ** pointer in-file offset to data area
  1409. ** pointer in-file size
  1410. ** (in-memory size excluding zerofill sections)
  1411. ** int maximum vm protection
  1412. ** int initial vm protection
  1413. ** uint32_t number of sections
  1414. ** uint32_t flags
  1415. **
  1416. ** section commands
  1417. ** char[16] section name
  1418. ** char[16] segment name
  1419. ** pointer in-memory offset
  1420. ** pointer in-memory size
  1421. ** uint32_t in-file offset
  1422. ** uint32_t alignment
  1423. ** (irrelevant in MH_OBJECT)
  1424. ** uint32_t in-file offset of relocation entires
  1425. ** uint32_t number of relocations
  1426. ** uint32_t flags
  1427. ** uint32_t reserved
  1428. ** uint32_t reserved
  1429. **
  1430. ** symbol table command
  1431. ** uint32_t command type == LC_SYMTAB
  1432. ** uint32_t size of load command
  1433. ** uint32_t symbol table offset
  1434. ** uint32_t number of symbol table entries
  1435. ** uint32_t string table offset
  1436. ** uint32_t string table size
  1437. **
  1438. ** raw section data
  1439. **
  1440. ** padding to pointer boundary
  1441. **
  1442. ** relocation data (struct reloc)
  1443. ** int32_t offset
  1444. ** uint data (symbolnum, pcrel, length, extern, type)
  1445. **
  1446. ** symbol table data (struct nlist)
  1447. ** int32_t string table entry number
  1448. ** uint8_t type
  1449. ** (extern, absolute, defined in section)
  1450. ** uint8_t section
  1451. ** (0 for global symbols, section number of definition (>= 1, <=
  1452. ** 254) for local symbols, size of variable for common symbols
  1453. ** [type == extern])
  1454. ** int16_t description
  1455. ** (for stab debugging format)
  1456. ** pointer value (i.e. file offset) of symbol or stab offset
  1457. **
  1458. ** string table data
  1459. ** list of null-terminated strings
  1460. */
  1461. /* Emit the Mach-O header. */
  1462. macho_write_header();
  1463. offset = fmt.header_size + head_sizeofcmds;
  1464. /* emit the segment load command */
  1465. if (seg_nsects > 0)
  1466. offset = macho_write_segment (offset);
  1467. else
  1468. nasm_error(ERR_WARNING, "no sections?");
  1469. if (nsyms > 0) {
  1470. /* write out symbol command */
  1471. fwriteint32_t(LC_SYMTAB, ofile); /* cmd == LC_SYMTAB */
  1472. fwriteint32_t(MACHO_SYMCMD_SIZE, ofile); /* size of load command */
  1473. fwriteint32_t(offset, ofile); /* symbol table offset */
  1474. fwriteint32_t(nsyms, ofile); /* number of symbol
  1475. ** table entries */
  1476. offset += nsyms * fmt.nlist_size;
  1477. fwriteint32_t(offset, ofile); /* string table offset */
  1478. fwriteint32_t(strslen, ofile); /* string table size */
  1479. }
  1480. /* emit section data */
  1481. if (seg_nsects > 0)
  1482. macho_write_section ();
  1483. /* emit symbol table if we have symbols */
  1484. if (nsyms > 0)
  1485. macho_write_symtab ();
  1486. /* we don't need to pad here, we are already aligned */
  1487. /* emit string table */
  1488. saa_fpwrite(strs, ofile);
  1489. }
  1490. /* We do quite a bit here, starting with finalizing all of the data
  1491. for the object file, writing, and then freeing all of the data from
  1492. the file. */
  1493. static void macho_cleanup(void)
  1494. {
  1495. struct section *s;
  1496. struct reloc *r;
  1497. struct symbol *sym;
  1498. dfmt->cleanup();
  1499. /* Sort all symbols. */
  1500. macho_layout_symbols (&nsyms, &strslen);
  1501. /* Fixup relocation entries */
  1502. for (s = sects; s != NULL; s = s->next) {
  1503. macho_fixup_relocs (s->relocs);
  1504. }
  1505. /* First calculate and finalize needed values. */
  1506. macho_calculate_sizes();
  1507. macho_write();
  1508. /* free up everything */
  1509. while (sects->next) {
  1510. s = sects;
  1511. sects = sects->next;
  1512. saa_free(s->data);
  1513. while (s->relocs != NULL) {
  1514. r = s->relocs;
  1515. s->relocs = s->relocs->next;
  1516. nasm_free(r);
  1517. }
  1518. nasm_free(s);
  1519. }
  1520. saa_free(strs);
  1521. raa_free(extsyms);
  1522. while (syms) {
  1523. sym = syms;
  1524. syms = syms->next;
  1525. nasm_free (sym);
  1526. }
  1527. nasm_free(extdefsyms);
  1528. nasm_free(undefsyms);
  1529. nasm_free(sectstab);
  1530. raa_free_ptr(section_by_index);
  1531. hash_free(&section_by_name);
  1532. }
  1533. static bool macho_set_section_attribute_by_symbol(const char *label, uint32_t flags)
  1534. {
  1535. struct section *s;
  1536. int32_t nasm_seg;
  1537. int64_t offset;
  1538. if (!lookup_label(label, &nasm_seg, &offset)) {
  1539. nasm_error(ERR_NONFATAL, "unknown symbol `%s' in no_dead_strip", label);
  1540. return false;
  1541. }
  1542. s = get_section_by_index(nasm_seg);
  1543. if (!s) {
  1544. nasm_error(ERR_NONFATAL, "symbol `%s' is external or absolute", label);
  1545. return false;
  1546. }
  1547. s->flags |= flags;
  1548. return true;
  1549. }
  1550. /*
  1551. * Mark a symbol for no dead stripping
  1552. */
  1553. static enum directive_result macho_no_dead_strip(const char *labels)
  1554. {
  1555. char *s, *p, *ep;
  1556. char ec;
  1557. enum directive_result rv = DIRR_ERROR;
  1558. bool real = passn > 1;
  1559. p = s = nasm_strdup(labels);
  1560. while (*p) {
  1561. ep = nasm_skip_identifier(p);
  1562. if (!ep) {
  1563. nasm_error(ERR_NONFATAL, "invalid symbol in NO_DEAD_STRIP");
  1564. goto err;
  1565. }
  1566. ec = *ep;
  1567. if (ec && ec != ',' && !nasm_isspace(ec)) {
  1568. nasm_error(ERR_NONFATAL, "cannot parse contents after symbol");
  1569. goto err;
  1570. }
  1571. *ep = '\0';
  1572. if (real) {
  1573. if (!macho_set_section_attribute_by_symbol(p, S_ATTR_NO_DEAD_STRIP))
  1574. rv = DIRR_ERROR;
  1575. }
  1576. *ep = ec;
  1577. p = nasm_skip_spaces(ep);
  1578. if (*p == ',')
  1579. p = nasm_skip_spaces(++p);
  1580. }
  1581. rv = DIRR_OK;
  1582. err:
  1583. nasm_free(s);
  1584. return rv;
  1585. }
  1586. /*
  1587. * Mach-O pragmas
  1588. */
  1589. static enum directive_result
  1590. macho_pragma(const struct pragma *pragma)
  1591. {
  1592. bool real = passn > 1;
  1593. switch (pragma->opcode) {
  1594. case D_SUBSECTIONS_VIA_SYMBOLS:
  1595. if (*pragma->tail)
  1596. return DIRR_BADPARAM;
  1597. if (real)
  1598. head_flags |= MH_SUBSECTIONS_VIA_SYMBOLS;
  1599. /* Jmp-match optimization conflicts */
  1600. optimizing.flag |= OPTIM_DISABLE_JMP_MATCH;
  1601. return DIRR_OK;
  1602. case D_NO_DEAD_STRIP:
  1603. return macho_no_dead_strip(pragma->tail);
  1604. default:
  1605. return DIRR_UNKNOWN; /* Not a Mach-O directive */
  1606. }
  1607. }
  1608. static const struct pragma_facility macho_pragma_list[] = {
  1609. { "macho", macho_pragma },
  1610. { NULL, macho_pragma } /* Implements macho32/macho64 namespaces */
  1611. };
  1612. static void macho_dbg_generate(void)
  1613. {
  1614. uint8_t *p_buf = NULL, *p_buf_base = NULL;
  1615. size_t saa_len = 0, high_addr = 0, total_len = 0;
  1616. struct section *p_section = NULL;
  1617. /* calculated at debug_str and referenced at debug_info */
  1618. uint32_t producer_str_offset = 0, module_str_offset = 0, dir_str_offset = 0;
  1619. /* debug section defines */
  1620. {
  1621. int bits = 0;
  1622. macho_section(".debug_abbrev", 0, &bits);
  1623. macho_section(".debug_info", 0, &bits);
  1624. macho_section(".debug_line", 0, &bits);
  1625. macho_section(".debug_str", 0, &bits);
  1626. }
  1627. /* dw section walk to find high_addr and total_len */
  1628. {
  1629. struct dw_sect_list *p_sect;
  1630. list_for_each(p_sect, dw_head_sect) {
  1631. uint64_t offset = get_section_by_index(p_sect->section)->size;
  1632. struct SAA *p_linep = p_sect->psaa;
  1633. saa_write8(p_linep, 2); /* std op 2 */
  1634. saa_write8(p_linep, offset - p_sect->offset);
  1635. saa_write8(p_linep, DW_LNS_extended_op);
  1636. saa_write8(p_linep, 1); /* operand length */
  1637. saa_write8(p_linep, DW_LNE_end_sequence);
  1638. total_len += p_linep->datalen;
  1639. high_addr += offset;
  1640. }
  1641. }
  1642. /* debug line */
  1643. {
  1644. struct dw_sect_list *p_sect;
  1645. size_t linep_off, buf_size;
  1646. struct SAA *p_lines = saa_init(1L);
  1647. struct dir_list *p_dir;
  1648. struct file_list *p_file;
  1649. p_section = get_section_by_name("__DWARF", "__debug_line");
  1650. nasm_assert(p_section != NULL);
  1651. saa_write8(p_lines, 1); /* minimum instruction length */
  1652. saa_write8(p_lines, 1); /* initial value of "is_stmt" */
  1653. saa_write8(p_lines, DW_LN_BASE); /* line base */
  1654. saa_write8(p_lines, DW_LN_RANGE); /* line range */
  1655. saa_write8(p_lines, DW_OPCODE_BASE); /* opcode base */
  1656. saa_write8(p_lines, 0); /* std opcode 1 length */
  1657. saa_write8(p_lines, 1); /* std opcode 2 length */
  1658. saa_write8(p_lines, 1); /* std opcode 3 length */
  1659. saa_write8(p_lines, 1); /* std opcode 4 length */
  1660. saa_write8(p_lines, 1); /* std opcode 5 length */
  1661. saa_write8(p_lines, 0); /* std opcode 6 length */
  1662. saa_write8(p_lines, 0); /* std opcode 7 length */
  1663. saa_write8(p_lines, 0); /* std opcode 8 length */
  1664. saa_write8(p_lines, 1); /* std opcode 9 length */
  1665. saa_write8(p_lines, 0); /* std opcode 10 length */
  1666. saa_write8(p_lines, 0); /* std opcode 11 length */
  1667. saa_write8(p_lines, 1); /* std opcode 12 length */
  1668. list_for_each(p_dir, dw_head_dir) {
  1669. saa_wcstring(p_lines, p_dir->dir_name);
  1670. }
  1671. saa_write8(p_lines, 0); /* end of table */
  1672. list_for_each(p_file, dw_head_file) {
  1673. saa_wcstring(p_lines, p_file->file_name);
  1674. saa_write8(p_lines, p_file->dir->dir); /* directory id */
  1675. saa_write8(p_lines, 0); /* time */
  1676. saa_write8(p_lines, 0); /* size */
  1677. }
  1678. saa_write8(p_lines, 0); /* end of table */
  1679. linep_off = p_lines->datalen;
  1680. /* 10 bytes for initial & prolong length, and dwarf version info */
  1681. buf_size = saa_len = linep_off + total_len + 10;
  1682. p_buf_base = p_buf = nasm_malloc(buf_size);
  1683. WRITELONG(p_buf, saa_len - 4); /* initial length; size excluding itself */
  1684. WRITESHORT(p_buf, 2); /* dwarf version */
  1685. WRITELONG(p_buf, linep_off); /* prolong length */
  1686. saa_rnbytes(p_lines, p_buf, linep_off);
  1687. p_buf += linep_off;
  1688. saa_free(p_lines);
  1689. list_for_each(p_sect, dw_head_sect) {
  1690. struct SAA *p_linep = p_sect->psaa;
  1691. saa_len = p_linep->datalen;
  1692. saa_rnbytes(p_linep, p_buf, saa_len);
  1693. p_buf += saa_len;
  1694. saa_free(p_linep);
  1695. }
  1696. macho_output(p_section->index, p_buf_base, OUT_RAWDATA, buf_size, NO_SEG, 0);
  1697. nasm_free(p_buf_base);
  1698. }
  1699. /* string section */
  1700. {
  1701. struct SAA *p_str = saa_init(1L);
  1702. char *cur_path = nasm_realpath(module_name);
  1703. char *cur_file = nasm_basename(cur_path);
  1704. char *cur_dir = nasm_dirname(cur_path);
  1705. p_section = get_section_by_name("__DWARF", "__debug_str");
  1706. nasm_assert(p_section != NULL);
  1707. producer_str_offset = 0;
  1708. module_str_offset = dir_str_offset = saa_wcstring(p_str, nasm_signature);
  1709. dir_str_offset += saa_wcstring(p_str, cur_file);
  1710. saa_wcstring(p_str, cur_dir);
  1711. saa_len = p_str->datalen;
  1712. p_buf = nasm_malloc(saa_len);
  1713. saa_rnbytes(p_str, p_buf, saa_len);
  1714. macho_output(p_section->index, p_buf, OUT_RAWDATA, saa_len, NO_SEG, 0);
  1715. nasm_free(cur_path);
  1716. nasm_free(cur_file);
  1717. nasm_free(cur_dir);
  1718. saa_free(p_str);
  1719. nasm_free(p_buf);
  1720. }
  1721. /* debug info */
  1722. {
  1723. struct SAA *p_info = saa_init(1L);
  1724. p_section = get_section_by_name("__DWARF", "__debug_info");
  1725. nasm_assert(p_section != NULL);
  1726. /* size will be overwritten once determined, so skip in p_info layout */
  1727. saa_write16(p_info, 2); /* dwarf version */
  1728. saa_write32(p_info, 0); /* offset info abbrev */
  1729. saa_write8(p_info, (ofmt == &of_macho64) ? 8 : 4); /* pointer size */
  1730. saa_write8(p_info, 1); /* abbrev entry number */
  1731. saa_write32(p_info, producer_str_offset); /* offset from string table for DW_AT_producer */
  1732. saa_write16(p_info, DW_LANG_Mips_Assembler); /* DW_AT_language */
  1733. saa_write32(p_info, module_str_offset); /* offset from string table for DW_AT_name */
  1734. saa_write32(p_info, dir_str_offset); /* offset from string table for DW_AT_comp_dir */
  1735. saa_write32(p_info, 0); /* DW_AT_stmt_list */
  1736. if (ofmt == &of_macho64) {
  1737. saa_write64(p_info, 0); /* DW_AT_low_pc */
  1738. saa_write64(p_info, high_addr); /* DW_AT_high_pc */
  1739. } else {
  1740. saa_write32(p_info, 0); /* DW_AT_low_pc */
  1741. saa_write32(p_info, high_addr); /* DW_AT_high_pc */
  1742. }
  1743. saa_write8(p_info, 2); /* abbrev entry number */
  1744. if (ofmt == &of_macho64) {
  1745. saa_write64(p_info, 0); /* DW_AT_low_pc */
  1746. saa_write64(p_info, 0); /* DW_AT_frame_base */
  1747. } else {
  1748. saa_write32(p_info, 0); /* DW_AT_low_pc */
  1749. saa_write32(p_info, 0); /* DW_AT_frame_base */
  1750. }
  1751. saa_write8(p_info, DW_END_default);
  1752. saa_len = p_info->datalen;
  1753. p_buf_base = p_buf = nasm_malloc(saa_len + 4); /* 4B for size info */
  1754. WRITELONG(p_buf, saa_len);
  1755. saa_rnbytes(p_info, p_buf, saa_len);
  1756. macho_output(p_section->index, p_buf_base, OUT_RAWDATA, saa_len + 4, NO_SEG, 0);
  1757. saa_free(p_info);
  1758. nasm_free(p_buf_base);
  1759. }
  1760. /* abbrev section */
  1761. {
  1762. struct SAA *p_abbrev = saa_init(1L);
  1763. p_section = get_section_by_name("__DWARF", "__debug_abbrev");
  1764. nasm_assert(p_section != NULL);
  1765. saa_write8(p_abbrev, 1); /* entry number */
  1766. saa_write8(p_abbrev, DW_TAG_compile_unit);
  1767. saa_write8(p_abbrev, DW_CHILDREN_yes);
  1768. saa_write8(p_abbrev, DW_AT_producer);
  1769. saa_write8(p_abbrev, DW_FORM_strp);
  1770. saa_write8(p_abbrev, DW_AT_language);
  1771. saa_write8(p_abbrev, DW_FORM_data2);
  1772. saa_write8(p_abbrev, DW_AT_name);
  1773. saa_write8(p_abbrev, DW_FORM_strp);
  1774. saa_write8(p_abbrev, DW_AT_comp_dir);
  1775. saa_write8(p_abbrev, DW_FORM_strp);
  1776. saa_write8(p_abbrev, DW_AT_stmt_list);
  1777. saa_write8(p_abbrev, DW_FORM_data4);
  1778. saa_write8(p_abbrev, DW_AT_low_pc);
  1779. saa_write8(p_abbrev, DW_FORM_addr);
  1780. saa_write8(p_abbrev, DW_AT_high_pc);
  1781. saa_write8(p_abbrev, DW_FORM_addr);
  1782. saa_write16(p_abbrev, DW_END_default);
  1783. saa_write8(p_abbrev, 2); /* entry number */
  1784. saa_write8(p_abbrev, DW_TAG_subprogram);
  1785. saa_write8(p_abbrev, DW_CHILDREN_no);
  1786. saa_write8(p_abbrev, DW_AT_low_pc);
  1787. saa_write8(p_abbrev, DW_FORM_addr);
  1788. saa_write8(p_abbrev, DW_AT_frame_base);
  1789. saa_write8(p_abbrev, DW_FORM_addr);
  1790. saa_write16(p_abbrev, DW_END_default);
  1791. saa_write8(p_abbrev, 0); /* Terminal zero entry */
  1792. saa_len = p_abbrev->datalen;
  1793. p_buf = nasm_malloc(saa_len);
  1794. saa_rnbytes(p_abbrev, p_buf, saa_len);
  1795. macho_output(p_section->index, p_buf, OUT_RAWDATA, saa_len, NO_SEG, 0);
  1796. saa_free(p_abbrev);
  1797. nasm_free(p_buf);
  1798. }
  1799. }
  1800. static void new_file_list (const char *file_name, const char *dir_name)
  1801. {
  1802. struct dir_list *dir_list;
  1803. bool need_new_dir_list = true;
  1804. nasm_new(dw_cur_file);
  1805. dw_cur_file->file = ++dw_num_files;
  1806. dw_cur_file->file_name = file_name;
  1807. if(!dw_head_file) {
  1808. dw_head_file = dw_cur_file;
  1809. } else {
  1810. *dw_last_file_next = dw_cur_file;
  1811. }
  1812. dw_last_file_next = &(dw_cur_file->next);
  1813. if(dw_head_dir) {
  1814. list_for_each(dir_list, dw_head_dir) {
  1815. if(!(strcmp(dir_name, dir_list->dir_name))) {
  1816. dw_cur_file->dir = dir_list;
  1817. need_new_dir_list = false;
  1818. break;
  1819. }
  1820. }
  1821. }
  1822. if(need_new_dir_list)
  1823. {
  1824. nasm_new(dir_list);
  1825. dir_list->dir = dw_num_dirs++;
  1826. dir_list->dir_name = dir_name;
  1827. if(!dw_head_dir) {
  1828. dw_head_dir = dir_list;
  1829. } else {
  1830. *dw_last_dir_next = dir_list;
  1831. }
  1832. dw_last_dir_next = &(dir_list->next);
  1833. dw_cur_file->dir = dir_list;
  1834. }
  1835. }
  1836. static void macho_dbg_init(void)
  1837. {
  1838. }
  1839. static void macho_dbg_linenum(const char *file_name, int32_t line_num, int32_t segto)
  1840. {
  1841. bool need_new_list = true;
  1842. const char *cur_file = nasm_basename(file_name);
  1843. const char *cur_dir = nasm_dirname(file_name);
  1844. (void)segto;
  1845. if(!dw_cur_file || strcmp(cur_file, dw_cur_file->file_name) ||
  1846. strcmp(cur_dir, dw_cur_file->dir->dir_name)) {
  1847. if(dw_head_file) {
  1848. struct file_list *match;
  1849. list_for_each(match, dw_head_file) {
  1850. if(!(strcmp(cur_file, match->file_name)) &&
  1851. !(strcmp(cur_dir, match->dir->dir_name))) {
  1852. dw_cur_file = match;
  1853. dw_cur_file->dir = match->dir;
  1854. need_new_list = false;
  1855. break;
  1856. }
  1857. }
  1858. }
  1859. if(need_new_list) {
  1860. new_file_list(cur_file, cur_dir);
  1861. }
  1862. }
  1863. dbg_immcall = true;
  1864. cur_line = line_num;
  1865. }
  1866. static void macho_dbg_output(int type, void *param)
  1867. {
  1868. struct section_info *sinfo_param = (struct section_info *)param;
  1869. int32_t secto = sinfo_param->secto;
  1870. bool need_new_sect = false;
  1871. struct SAA *p_linep = NULL;
  1872. (void)type;
  1873. if(!(dw_cur_sect && (dw_cur_sect->section == secto))) {
  1874. need_new_sect = true;
  1875. if(dw_head_sect) {
  1876. struct dw_sect_list *match = dw_head_sect;
  1877. uint32_t idx = 0;
  1878. for(; idx < dw_num_sects; idx++) {
  1879. if(match->section == secto) {
  1880. dw_cur_sect = match;
  1881. need_new_sect = false;
  1882. break;
  1883. }
  1884. match = match->next;
  1885. }
  1886. }
  1887. }
  1888. if(need_new_sect) {
  1889. nasm_new(dw_cur_sect);
  1890. dw_num_sects ++;
  1891. p_linep = dw_cur_sect->psaa = saa_init(1L);
  1892. dw_cur_sect->line = dw_cur_sect->file = 1;
  1893. dw_cur_sect->offset = 0;
  1894. dw_cur_sect->next = NULL;
  1895. dw_cur_sect->section = secto;
  1896. saa_write8(p_linep, DW_LNS_extended_op);
  1897. saa_write8(p_linep, (ofmt == &of_macho64) ? 9 : 5);
  1898. saa_write8(p_linep, DW_LNE_set_address);
  1899. if (ofmt == &of_macho64) {
  1900. saa_write64(p_linep, 0);
  1901. } else {
  1902. saa_write32(p_linep, 0);
  1903. }
  1904. if(!dw_head_sect) {
  1905. dw_head_sect = dw_last_sect = dw_cur_sect;
  1906. } else {
  1907. dw_last_sect->next = dw_cur_sect;
  1908. dw_last_sect = dw_cur_sect;
  1909. }
  1910. }
  1911. if(dbg_immcall == true) {
  1912. int32_t line_delta = cur_line - dw_cur_sect->line;
  1913. int32_t offset_delta = sinfo_param->size - dw_cur_sect->offset;
  1914. uint32_t cur_file = dw_cur_file->file;
  1915. p_linep = dw_cur_sect->psaa;
  1916. if(cur_file != dw_cur_sect->file) {
  1917. saa_write8(p_linep, DW_LNS_set_file);
  1918. saa_write8(p_linep, cur_file);
  1919. dw_cur_sect->file = cur_file;
  1920. }
  1921. if(line_delta) {
  1922. int special_opcode = (line_delta - DW_LN_BASE) + (DW_LN_RANGE * offset_delta) +
  1923. DW_OPCODE_BASE;
  1924. if((line_delta >= DW_LN_BASE) && (line_delta < DW_MAX_LN) &&
  1925. (special_opcode < DW_MAX_SP_OPCODE)) {
  1926. saa_write8(p_linep, special_opcode);
  1927. } else {
  1928. saa_write8(p_linep, DW_LNS_advance_line);
  1929. saa_wleb128s(p_linep, line_delta);
  1930. if(offset_delta) {
  1931. saa_write8(p_linep, DW_LNS_advance_pc);
  1932. saa_wleb128u(p_linep, offset_delta);
  1933. }
  1934. saa_write8(p_linep, DW_LNS_copy);
  1935. }
  1936. dw_cur_sect->line = cur_line;
  1937. dw_cur_sect->offset = sinfo_param->size;
  1938. }
  1939. dbg_immcall = false;
  1940. }
  1941. }
  1942. static void macho_dbg_cleanup(void)
  1943. {
  1944. /* dwarf sectors generation */
  1945. macho_dbg_generate();
  1946. {
  1947. struct dw_sect_list *p_sect = dw_head_sect;
  1948. struct file_list *p_file = dw_head_file;
  1949. uint32_t idx = 0;
  1950. for(; idx < dw_num_sects; idx++) {
  1951. struct dw_sect_list *next = p_sect->next;
  1952. nasm_free(p_sect);
  1953. p_sect = next;
  1954. }
  1955. for(idx = 0; idx < dw_num_files; idx++) {
  1956. struct file_list *next = p_file->next;
  1957. nasm_free(p_file);
  1958. p_file = next;
  1959. }
  1960. }
  1961. }
  1962. #ifdef OF_MACHO32
  1963. static const struct macho_fmt macho32_fmt = {
  1964. 4,
  1965. MH_MAGIC,
  1966. CPU_TYPE_I386,
  1967. LC_SEGMENT,
  1968. MACHO_HEADER_SIZE,
  1969. MACHO_SEGCMD_SIZE,
  1970. MACHO_SECTCMD_SIZE,
  1971. MACHO_NLIST_SIZE,
  1972. RL_MAX_32,
  1973. GENERIC_RELOC_VANILLA,
  1974. GENERIC_RELOC_VANILLA,
  1975. GENERIC_RELOC_TLV,
  1976. false /* Allow segment-relative relocations */
  1977. };
  1978. static void macho32_init(void)
  1979. {
  1980. fmt = macho32_fmt;
  1981. macho_init();
  1982. macho_gotpcrel_sect = NO_SEG;
  1983. }
  1984. static const struct dfmt macho32_df_dwarf = {
  1985. "MachO32 (i386) dwarf debug format for Darwin/MacOS",
  1986. "dwarf",
  1987. macho_dbg_init,
  1988. macho_dbg_linenum,
  1989. null_debug_deflabel,
  1990. null_debug_directive,
  1991. null_debug_typevalue,
  1992. macho_dbg_output,
  1993. macho_dbg_cleanup,
  1994. NULL /*pragma list*/
  1995. };
  1996. static const struct dfmt * const macho32_df_arr[2] =
  1997. { &macho32_df_dwarf, NULL };
  1998. const struct ofmt of_macho32 = {
  1999. "NeXTstep/OpenStep/Rhapsody/Darwin/MacOS X (i386) object files",
  2000. "macho32",
  2001. ".o",
  2002. 0,
  2003. 32,
  2004. macho32_df_arr,
  2005. &macho32_df_dwarf,
  2006. macho_stdmac,
  2007. macho32_init,
  2008. null_reset,
  2009. nasm_do_legacy_output,
  2010. macho_output,
  2011. macho_symdef,
  2012. macho_section,
  2013. macho_herelabel,
  2014. macho_sectalign,
  2015. macho_segbase,
  2016. null_directive,
  2017. macho_cleanup,
  2018. macho_pragma_list
  2019. };
  2020. #endif
  2021. #ifdef OF_MACHO64
  2022. static const struct macho_fmt macho64_fmt = {
  2023. 8,
  2024. MH_MAGIC_64,
  2025. CPU_TYPE_X86_64,
  2026. LC_SEGMENT_64,
  2027. MACHO_HEADER64_SIZE,
  2028. MACHO_SEGCMD64_SIZE,
  2029. MACHO_SECTCMD64_SIZE,
  2030. MACHO_NLIST64_SIZE,
  2031. RL_MAX_64,
  2032. X86_64_RELOC_UNSIGNED,
  2033. X86_64_RELOC_SIGNED,
  2034. X86_64_RELOC_TLV,
  2035. true /* Force symbol-relative relocations */
  2036. };
  2037. static void macho64_init(void)
  2038. {
  2039. fmt = macho64_fmt;
  2040. macho_init();
  2041. /* add special symbol for ..gotpcrel */
  2042. macho_gotpcrel_sect = seg_alloc() + 1;
  2043. backend_label("..gotpcrel", macho_gotpcrel_sect, 0L);
  2044. }
  2045. static const struct dfmt macho64_df_dwarf = {
  2046. "MachO64 (x86-64) dwarf debug format for Darwin/MacOS",
  2047. "dwarf",
  2048. macho_dbg_init,
  2049. macho_dbg_linenum,
  2050. null_debug_deflabel,
  2051. null_debug_directive,
  2052. null_debug_typevalue,
  2053. macho_dbg_output,
  2054. macho_dbg_cleanup,
  2055. NULL /*pragma list*/
  2056. };
  2057. static const struct dfmt * const macho64_df_arr[2] =
  2058. { &macho64_df_dwarf, NULL };
  2059. const struct ofmt of_macho64 = {
  2060. "NeXTstep/OpenStep/Rhapsody/Darwin/MacOS X (x86_64) object files",
  2061. "macho64",
  2062. ".o",
  2063. 0,
  2064. 64,
  2065. macho64_df_arr,
  2066. &macho64_df_dwarf,
  2067. macho_stdmac,
  2068. macho64_init,
  2069. null_reset,
  2070. nasm_do_legacy_output,
  2071. macho_output,
  2072. macho_symdef,
  2073. macho_section,
  2074. macho_herelabel,
  2075. macho_sectalign,
  2076. macho_segbase,
  2077. null_directive,
  2078. macho_cleanup,
  2079. macho_pragma_list,
  2080. };
  2081. #endif
  2082. #endif
  2083. /*
  2084. * Local Variables:
  2085. * mode:c
  2086. * c-basic-offset:4
  2087. * End:
  2088. *
  2089. * end of file */